wpa_auth.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476
  1. /*
  2. * hostapd - IEEE 802.11i-2004 / WPA Authenticator
  3. * Copyright (c) 2004-2009, 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 "utils/includes.h"
  15. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include "utils/common.h"
  17. #include "utils/eloop.h"
  18. #include "utils/state_machine.h"
  19. #include "common/ieee802_11_defs.h"
  20. #include "crypto/aes_wrap.h"
  21. #include "crypto/crypto.h"
  22. #include "crypto/sha1.h"
  23. #include "crypto/sha256.h"
  24. #include "eapol_auth/eapol_auth_sm.h"
  25. #include "ap_config.h"
  26. #include "ieee802_11.h"
  27. #include "wpa_auth.h"
  28. #include "pmksa_cache_auth.h"
  29. #include "wpa_auth_i.h"
  30. #include "wpa_auth_ie.h"
  31. #define STATE_MACHINE_DATA struct wpa_state_machine
  32. #define STATE_MACHINE_DEBUG_PREFIX "WPA"
  33. #define STATE_MACHINE_ADDR sm->addr
  34. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
  35. static void wpa_sm_step(struct wpa_state_machine *sm);
  36. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
  37. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
  38. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  39. struct wpa_group *group);
  40. static void wpa_request_new_ptk(struct wpa_state_machine *sm);
  41. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  42. struct wpa_group *group);
  43. static const u32 dot11RSNAConfigGroupUpdateCount = 4;
  44. static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
  45. static const u32 eapol_key_timeout_first = 100; /* ms */
  46. static const u32 eapol_key_timeout_subseq = 1000; /* ms */
  47. /* TODO: make these configurable */
  48. static const int dot11RSNAConfigPMKLifetime = 43200;
  49. static const int dot11RSNAConfigPMKReauthThreshold = 70;
  50. static const int dot11RSNAConfigSATimeout = 60;
  51. static inline void wpa_auth_mic_failure_report(
  52. struct wpa_authenticator *wpa_auth, const u8 *addr)
  53. {
  54. if (wpa_auth->cb.mic_failure_report)
  55. wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
  56. }
  57. static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
  58. const u8 *addr, wpa_eapol_variable var,
  59. int value)
  60. {
  61. if (wpa_auth->cb.set_eapol)
  62. wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
  63. }
  64. static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
  65. const u8 *addr, wpa_eapol_variable var)
  66. {
  67. if (wpa_auth->cb.get_eapol == NULL)
  68. return -1;
  69. return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
  70. }
  71. static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
  72. const u8 *addr, const u8 *prev_psk)
  73. {
  74. if (wpa_auth->cb.get_psk == NULL)
  75. return NULL;
  76. return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
  77. }
  78. static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
  79. const u8 *addr, u8 *msk, size_t *len)
  80. {
  81. if (wpa_auth->cb.get_msk == NULL)
  82. return -1;
  83. return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
  84. }
  85. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  86. int vlan_id,
  87. enum wpa_alg alg, const u8 *addr, int idx,
  88. u8 *key, size_t key_len)
  89. {
  90. if (wpa_auth->cb.set_key == NULL)
  91. return -1;
  92. return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
  93. key, key_len);
  94. }
  95. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  96. const u8 *addr, int idx, u8 *seq)
  97. {
  98. if (wpa_auth->cb.get_seqnum == NULL)
  99. return -1;
  100. return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
  101. }
  102. static inline int
  103. wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
  104. const u8 *data, size_t data_len, int encrypt)
  105. {
  106. if (wpa_auth->cb.send_eapol == NULL)
  107. return -1;
  108. return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
  109. encrypt);
  110. }
  111. int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
  112. int (*cb)(struct wpa_state_machine *sm, void *ctx),
  113. void *cb_ctx)
  114. {
  115. if (wpa_auth->cb.for_each_sta == NULL)
  116. return 0;
  117. return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
  118. }
  119. int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
  120. int (*cb)(struct wpa_authenticator *a, void *ctx),
  121. void *cb_ctx)
  122. {
  123. if (wpa_auth->cb.for_each_auth == NULL)
  124. return 0;
  125. return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
  126. }
  127. void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  128. logger_level level, const char *txt)
  129. {
  130. if (wpa_auth->cb.logger == NULL)
  131. return;
  132. wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
  133. }
  134. void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  135. logger_level level, const char *fmt, ...)
  136. {
  137. char *format;
  138. int maxlen;
  139. va_list ap;
  140. if (wpa_auth->cb.logger == NULL)
  141. return;
  142. maxlen = os_strlen(fmt) + 100;
  143. format = os_malloc(maxlen);
  144. if (!format)
  145. return;
  146. va_start(ap, fmt);
  147. vsnprintf(format, maxlen, fmt, ap);
  148. va_end(ap);
  149. wpa_auth_logger(wpa_auth, addr, level, format);
  150. os_free(format);
  151. }
  152. static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
  153. const u8 *addr)
  154. {
  155. if (wpa_auth->cb.disconnect == NULL)
  156. return;
  157. wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
  158. WLAN_REASON_PREV_AUTH_NOT_VALID);
  159. }
  160. static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
  161. {
  162. int ret = 0;
  163. #ifdef CONFIG_IEEE80211R
  164. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  165. ret = 1;
  166. #endif /* CONFIG_IEEE80211R */
  167. #ifdef CONFIG_IEEE80211W
  168. if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
  169. ret = 1;
  170. #endif /* CONFIG_IEEE80211W */
  171. return ret;
  172. }
  173. static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
  174. {
  175. struct wpa_authenticator *wpa_auth = eloop_ctx;
  176. if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
  177. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  178. "initialization.");
  179. } else {
  180. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
  181. }
  182. if (wpa_auth->conf.wpa_gmk_rekey) {
  183. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  184. wpa_rekey_gmk, wpa_auth, NULL);
  185. }
  186. }
  187. static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
  188. {
  189. struct wpa_authenticator *wpa_auth = eloop_ctx;
  190. struct wpa_group *group;
  191. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
  192. for (group = wpa_auth->group; group; group = group->next) {
  193. group->GTKReKey = TRUE;
  194. do {
  195. group->changed = FALSE;
  196. wpa_group_sm_step(wpa_auth, group);
  197. } while (group->changed);
  198. }
  199. if (wpa_auth->conf.wpa_group_rekey) {
  200. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
  201. 0, wpa_rekey_gtk, wpa_auth, NULL);
  202. }
  203. }
  204. static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
  205. {
  206. struct wpa_authenticator *wpa_auth = eloop_ctx;
  207. struct wpa_state_machine *sm = timeout_ctx;
  208. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
  209. wpa_request_new_ptk(sm);
  210. wpa_sm_step(sm);
  211. }
  212. static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
  213. {
  214. if (sm->pmksa == ctx)
  215. sm->pmksa = NULL;
  216. return 0;
  217. }
  218. static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
  219. void *ctx)
  220. {
  221. struct wpa_authenticator *wpa_auth = ctx;
  222. wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
  223. }
  224. static void wpa_group_set_key_len(struct wpa_group *group, int cipher)
  225. {
  226. switch (cipher) {
  227. case WPA_CIPHER_CCMP:
  228. group->GTK_len = 16;
  229. break;
  230. case WPA_CIPHER_TKIP:
  231. group->GTK_len = 32;
  232. break;
  233. case WPA_CIPHER_WEP104:
  234. group->GTK_len = 13;
  235. break;
  236. case WPA_CIPHER_WEP40:
  237. group->GTK_len = 5;
  238. break;
  239. }
  240. }
  241. static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
  242. int vlan_id)
  243. {
  244. struct wpa_group *group;
  245. u8 buf[ETH_ALEN + 8 + sizeof(group)];
  246. u8 rkey[32];
  247. group = os_zalloc(sizeof(struct wpa_group));
  248. if (group == NULL)
  249. return NULL;
  250. group->GTKAuthenticator = TRUE;
  251. group->vlan_id = vlan_id;
  252. wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
  253. /* Counter = PRF-256(Random number, "Init Counter",
  254. * Local MAC Address || Time)
  255. */
  256. os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
  257. wpa_get_ntp_timestamp(buf + ETH_ALEN);
  258. os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
  259. if (os_get_random(rkey, sizeof(rkey)) ||
  260. os_get_random(group->GMK, WPA_GMK_LEN)) {
  261. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  262. "initialization.");
  263. os_free(group);
  264. return NULL;
  265. }
  266. sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
  267. group->Counter, WPA_NONCE_LEN);
  268. group->GInit = TRUE;
  269. wpa_group_sm_step(wpa_auth, group);
  270. group->GInit = FALSE;
  271. wpa_group_sm_step(wpa_auth, group);
  272. return group;
  273. }
  274. /**
  275. * wpa_init - Initialize WPA authenticator
  276. * @addr: Authenticator address
  277. * @conf: Configuration for WPA authenticator
  278. * @cb: Callback functions for WPA authenticator
  279. * Returns: Pointer to WPA authenticator data or %NULL on failure
  280. */
  281. struct wpa_authenticator * wpa_init(const u8 *addr,
  282. struct wpa_auth_config *conf,
  283. struct wpa_auth_callbacks *cb)
  284. {
  285. struct wpa_authenticator *wpa_auth;
  286. wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
  287. if (wpa_auth == NULL)
  288. return NULL;
  289. os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
  290. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  291. os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
  292. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  293. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  294. os_free(wpa_auth);
  295. return NULL;
  296. }
  297. wpa_auth->group = wpa_group_init(wpa_auth, 0);
  298. if (wpa_auth->group == NULL) {
  299. os_free(wpa_auth->wpa_ie);
  300. os_free(wpa_auth);
  301. return NULL;
  302. }
  303. wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
  304. wpa_auth);
  305. if (wpa_auth->pmksa == NULL) {
  306. wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
  307. os_free(wpa_auth->wpa_ie);
  308. os_free(wpa_auth);
  309. return NULL;
  310. }
  311. #ifdef CONFIG_IEEE80211R
  312. wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
  313. if (wpa_auth->ft_pmk_cache == NULL) {
  314. wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
  315. os_free(wpa_auth->wpa_ie);
  316. pmksa_cache_auth_deinit(wpa_auth->pmksa);
  317. os_free(wpa_auth);
  318. return NULL;
  319. }
  320. #endif /* CONFIG_IEEE80211R */
  321. if (wpa_auth->conf.wpa_gmk_rekey) {
  322. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  323. wpa_rekey_gmk, wpa_auth, NULL);
  324. }
  325. if (wpa_auth->conf.wpa_group_rekey) {
  326. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
  327. wpa_rekey_gtk, wpa_auth, NULL);
  328. }
  329. return wpa_auth;
  330. }
  331. /**
  332. * wpa_deinit - Deinitialize WPA authenticator
  333. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  334. */
  335. void wpa_deinit(struct wpa_authenticator *wpa_auth)
  336. {
  337. struct wpa_group *group, *prev;
  338. eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
  339. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  340. #ifdef CONFIG_PEERKEY
  341. while (wpa_auth->stsl_negotiations)
  342. wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
  343. #endif /* CONFIG_PEERKEY */
  344. pmksa_cache_auth_deinit(wpa_auth->pmksa);
  345. #ifdef CONFIG_IEEE80211R
  346. wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
  347. wpa_auth->ft_pmk_cache = NULL;
  348. #endif /* CONFIG_IEEE80211R */
  349. os_free(wpa_auth->wpa_ie);
  350. group = wpa_auth->group;
  351. while (group) {
  352. prev = group;
  353. group = group->next;
  354. os_free(prev);
  355. }
  356. os_free(wpa_auth);
  357. }
  358. /**
  359. * wpa_reconfig - Update WPA authenticator configuration
  360. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  361. * @conf: Configuration for WPA authenticator
  362. */
  363. int wpa_reconfig(struct wpa_authenticator *wpa_auth,
  364. struct wpa_auth_config *conf)
  365. {
  366. struct wpa_group *group;
  367. if (wpa_auth == NULL)
  368. return 0;
  369. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  370. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  371. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  372. return -1;
  373. }
  374. /*
  375. * Reinitialize GTK to make sure it is suitable for the new
  376. * configuration.
  377. */
  378. group = wpa_auth->group;
  379. wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
  380. group->GInit = TRUE;
  381. wpa_group_sm_step(wpa_auth, group);
  382. group->GInit = FALSE;
  383. wpa_group_sm_step(wpa_auth, group);
  384. return 0;
  385. }
  386. struct wpa_state_machine *
  387. wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
  388. {
  389. struct wpa_state_machine *sm;
  390. sm = os_zalloc(sizeof(struct wpa_state_machine));
  391. if (sm == NULL)
  392. return NULL;
  393. os_memcpy(sm->addr, addr, ETH_ALEN);
  394. sm->wpa_auth = wpa_auth;
  395. sm->group = wpa_auth->group;
  396. return sm;
  397. }
  398. void wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
  399. struct wpa_state_machine *sm)
  400. {
  401. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  402. return;
  403. #ifdef CONFIG_IEEE80211R
  404. if (sm->ft_completed) {
  405. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  406. "FT authentication already completed - do not "
  407. "start 4-way handshake");
  408. return;
  409. }
  410. #endif /* CONFIG_IEEE80211R */
  411. if (sm->started) {
  412. os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
  413. sm->ReAuthenticationRequest = TRUE;
  414. wpa_sm_step(sm);
  415. return;
  416. }
  417. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  418. "start authentication");
  419. sm->started = 1;
  420. sm->Init = TRUE;
  421. wpa_sm_step(sm);
  422. sm->Init = FALSE;
  423. sm->AuthenticationRequest = TRUE;
  424. wpa_sm_step(sm);
  425. }
  426. void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
  427. {
  428. /* WPA/RSN was not used - clear WPA state. This is needed if the STA
  429. * reassociates back to the same AP while the previous entry for the
  430. * STA has not yet been removed. */
  431. if (sm == NULL)
  432. return;
  433. sm->wpa_key_mgmt = 0;
  434. }
  435. static void wpa_free_sta_sm(struct wpa_state_machine *sm)
  436. {
  437. os_free(sm->last_rx_eapol_key);
  438. os_free(sm->wpa_ie);
  439. os_free(sm);
  440. }
  441. void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
  442. {
  443. if (sm == NULL)
  444. return;
  445. if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
  446. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  447. "strict rekeying - force GTK rekey since STA "
  448. "is leaving");
  449. eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
  450. eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
  451. NULL);
  452. }
  453. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  454. eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
  455. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  456. if (sm->in_step_loop) {
  457. /* Must not free state machine while wpa_sm_step() is running.
  458. * Freeing will be completed in the end of wpa_sm_step(). */
  459. wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
  460. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  461. sm->pending_deinit = 1;
  462. } else
  463. wpa_free_sta_sm(sm);
  464. }
  465. static void wpa_request_new_ptk(struct wpa_state_machine *sm)
  466. {
  467. if (sm == NULL)
  468. return;
  469. sm->PTKRequest = TRUE;
  470. sm->PTK_valid = 0;
  471. }
  472. static int wpa_replay_counter_valid(struct wpa_state_machine *sm,
  473. const u8 *replay_counter)
  474. {
  475. int i;
  476. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  477. if (!sm->key_replay[i].valid)
  478. break;
  479. if (os_memcmp(replay_counter, sm->key_replay[i].counter,
  480. WPA_REPLAY_COUNTER_LEN) == 0)
  481. return 1;
  482. }
  483. return 0;
  484. }
  485. void wpa_receive(struct wpa_authenticator *wpa_auth,
  486. struct wpa_state_machine *sm,
  487. u8 *data, size_t data_len)
  488. {
  489. struct ieee802_1x_hdr *hdr;
  490. struct wpa_eapol_key *key;
  491. u16 key_info, key_data_length;
  492. enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
  493. SMK_M1, SMK_M3, SMK_ERROR } msg;
  494. char *msgtxt;
  495. struct wpa_eapol_ie_parse kde;
  496. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  497. return;
  498. if (data_len < sizeof(*hdr) + sizeof(*key))
  499. return;
  500. hdr = (struct ieee802_1x_hdr *) data;
  501. key = (struct wpa_eapol_key *) (hdr + 1);
  502. key_info = WPA_GET_BE16(key->key_info);
  503. key_data_length = WPA_GET_BE16(key->key_data_length);
  504. if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
  505. wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
  506. "key_data overflow (%d > %lu)",
  507. key_data_length,
  508. (unsigned long) (data_len - sizeof(*hdr) -
  509. sizeof(*key)));
  510. return;
  511. }
  512. if (sm->wpa == WPA_VERSION_WPA2) {
  513. if (key->type != EAPOL_KEY_TYPE_RSN) {
  514. wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
  515. "unexpected type %d in RSN mode",
  516. key->type);
  517. return;
  518. }
  519. } else {
  520. if (key->type != EAPOL_KEY_TYPE_WPA) {
  521. wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
  522. "unexpected type %d in WPA mode",
  523. key->type);
  524. return;
  525. }
  526. }
  527. /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
  528. * are set */
  529. if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
  530. (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
  531. if (key_info & WPA_KEY_INFO_ERROR) {
  532. msg = SMK_ERROR;
  533. msgtxt = "SMK Error";
  534. } else {
  535. msg = SMK_M1;
  536. msgtxt = "SMK M1";
  537. }
  538. } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
  539. msg = SMK_M3;
  540. msgtxt = "SMK M3";
  541. } else if (key_info & WPA_KEY_INFO_REQUEST) {
  542. msg = REQUEST;
  543. msgtxt = "Request";
  544. } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
  545. msg = GROUP_2;
  546. msgtxt = "2/2 Group";
  547. } else if (key_data_length == 0) {
  548. msg = PAIRWISE_4;
  549. msgtxt = "4/4 Pairwise";
  550. } else {
  551. msg = PAIRWISE_2;
  552. msgtxt = "2/4 Pairwise";
  553. }
  554. /* TODO: key_info type validation for PeerKey */
  555. if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
  556. msg == GROUP_2) {
  557. u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  558. if (sm->pairwise == WPA_CIPHER_CCMP) {
  559. if (wpa_use_aes_cmac(sm) &&
  560. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  561. wpa_auth_logger(wpa_auth, sm->addr,
  562. LOGGER_WARNING,
  563. "advertised support for "
  564. "AES-128-CMAC, but did not "
  565. "use it");
  566. return;
  567. }
  568. if (!wpa_use_aes_cmac(sm) &&
  569. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  570. wpa_auth_logger(wpa_auth, sm->addr,
  571. LOGGER_WARNING,
  572. "did not use HMAC-SHA1-AES "
  573. "with CCMP");
  574. return;
  575. }
  576. }
  577. }
  578. if (key_info & WPA_KEY_INFO_REQUEST) {
  579. if (sm->req_replay_counter_used &&
  580. os_memcmp(key->replay_counter, sm->req_replay_counter,
  581. WPA_REPLAY_COUNTER_LEN) <= 0) {
  582. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
  583. "received EAPOL-Key request with "
  584. "replayed counter");
  585. return;
  586. }
  587. }
  588. if (!(key_info & WPA_KEY_INFO_REQUEST) &&
  589. !wpa_replay_counter_valid(sm, key->replay_counter)) {
  590. int i;
  591. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  592. "received EAPOL-Key %s with unexpected "
  593. "replay counter", msgtxt);
  594. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  595. if (!sm->key_replay[i].valid)
  596. break;
  597. wpa_hexdump(MSG_DEBUG, "pending replay counter",
  598. sm->key_replay[i].counter,
  599. WPA_REPLAY_COUNTER_LEN);
  600. }
  601. wpa_hexdump(MSG_DEBUG, "received replay counter",
  602. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  603. return;
  604. }
  605. switch (msg) {
  606. case PAIRWISE_2:
  607. if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
  608. sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
  609. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  610. "received EAPOL-Key msg 2/4 in "
  611. "invalid state (%d) - dropped",
  612. sm->wpa_ptk_state);
  613. return;
  614. }
  615. if (sm->wpa_ie == NULL ||
  616. sm->wpa_ie_len != key_data_length ||
  617. os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
  618. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  619. "WPA IE from (Re)AssocReq did not "
  620. "match with msg 2/4");
  621. if (sm->wpa_ie) {
  622. wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
  623. sm->wpa_ie, sm->wpa_ie_len);
  624. }
  625. wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
  626. (u8 *) (key + 1), key_data_length);
  627. /* MLME-DEAUTHENTICATE.request */
  628. wpa_sta_disconnect(wpa_auth, sm->addr);
  629. return;
  630. }
  631. break;
  632. case PAIRWISE_4:
  633. if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
  634. !sm->PTK_valid) {
  635. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  636. "received EAPOL-Key msg 4/4 in "
  637. "invalid state (%d) - dropped",
  638. sm->wpa_ptk_state);
  639. return;
  640. }
  641. break;
  642. case GROUP_2:
  643. if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
  644. || !sm->PTK_valid) {
  645. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  646. "received EAPOL-Key msg 2/2 in "
  647. "invalid state (%d) - dropped",
  648. sm->wpa_ptk_group_state);
  649. return;
  650. }
  651. break;
  652. #ifdef CONFIG_PEERKEY
  653. case SMK_M1:
  654. case SMK_M3:
  655. case SMK_ERROR:
  656. if (!wpa_auth->conf.peerkey) {
  657. wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
  658. "PeerKey use disabled - ignoring message");
  659. return;
  660. }
  661. if (!sm->PTK_valid) {
  662. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  663. "received EAPOL-Key msg SMK in "
  664. "invalid state - dropped");
  665. return;
  666. }
  667. break;
  668. #else /* CONFIG_PEERKEY */
  669. case SMK_M1:
  670. case SMK_M3:
  671. case SMK_ERROR:
  672. return; /* STSL disabled - ignore SMK messages */
  673. #endif /* CONFIG_PEERKEY */
  674. case REQUEST:
  675. break;
  676. }
  677. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  678. "received EAPOL-Key frame (%s)", msgtxt);
  679. if (key_info & WPA_KEY_INFO_ACK) {
  680. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  681. "received invalid EAPOL-Key: Key Ack set");
  682. return;
  683. }
  684. if (!(key_info & WPA_KEY_INFO_MIC)) {
  685. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  686. "received invalid EAPOL-Key: Key MIC not set");
  687. return;
  688. }
  689. sm->MICVerified = FALSE;
  690. if (sm->PTK_valid) {
  691. if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
  692. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  693. "received EAPOL-Key with invalid MIC");
  694. return;
  695. }
  696. sm->MICVerified = TRUE;
  697. eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
  698. }
  699. if (key_info & WPA_KEY_INFO_REQUEST) {
  700. if (sm->MICVerified) {
  701. sm->req_replay_counter_used = 1;
  702. os_memcpy(sm->req_replay_counter, key->replay_counter,
  703. WPA_REPLAY_COUNTER_LEN);
  704. } else {
  705. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  706. "received EAPOL-Key request with "
  707. "invalid MIC");
  708. return;
  709. }
  710. /*
  711. * TODO: should decrypt key data field if encryption was used;
  712. * even though MAC address KDE is not normally encrypted,
  713. * supplicant is allowed to encrypt it.
  714. */
  715. if (msg == SMK_ERROR) {
  716. #ifdef CONFIG_PEERKEY
  717. wpa_smk_error(wpa_auth, sm, key);
  718. #endif /* CONFIG_PEERKEY */
  719. return;
  720. } else if (key_info & WPA_KEY_INFO_ERROR) {
  721. /* Supplicant reported a Michael MIC error */
  722. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  723. "received EAPOL-Key Error Request "
  724. "(STA detected Michael MIC failure)");
  725. wpa_auth_mic_failure_report(wpa_auth, sm->addr);
  726. sm->dot11RSNAStatsTKIPRemoteMICFailures++;
  727. wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
  728. /* Error report is not a request for a new key
  729. * handshake, but since Authenticator may do it, let's
  730. * change the keys now anyway. */
  731. wpa_request_new_ptk(sm);
  732. } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  733. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  734. "received EAPOL-Key Request for new "
  735. "4-Way Handshake");
  736. wpa_request_new_ptk(sm);
  737. #ifdef CONFIG_PEERKEY
  738. } else if (msg == SMK_M1) {
  739. wpa_smk_m1(wpa_auth, sm, key);
  740. #endif /* CONFIG_PEERKEY */
  741. } else if (key_data_length > 0 &&
  742. wpa_parse_kde_ies((const u8 *) (key + 1),
  743. key_data_length, &kde) == 0 &&
  744. kde.mac_addr) {
  745. } else {
  746. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  747. "received EAPOL-Key Request for GTK "
  748. "rekeying");
  749. /* FIX: why was this triggering PTK rekeying for the
  750. * STA that requested Group Key rekeying?? */
  751. /* wpa_request_new_ptk(sta->wpa_sm); */
  752. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  753. wpa_rekey_gtk(wpa_auth, NULL);
  754. }
  755. } else {
  756. /* Do not allow the same key replay counter to be reused. This
  757. * does also invalidate all other pending replay counters if
  758. * retransmissions were used, i.e., we will only process one of
  759. * the pending replies and ignore rest if more than one is
  760. * received. */
  761. sm->key_replay[0].valid = FALSE;
  762. }
  763. #ifdef CONFIG_PEERKEY
  764. if (msg == SMK_M3) {
  765. wpa_smk_m3(wpa_auth, sm, key);
  766. return;
  767. }
  768. #endif /* CONFIG_PEERKEY */
  769. os_free(sm->last_rx_eapol_key);
  770. sm->last_rx_eapol_key = os_malloc(data_len);
  771. if (sm->last_rx_eapol_key == NULL)
  772. return;
  773. os_memcpy(sm->last_rx_eapol_key, data, data_len);
  774. sm->last_rx_eapol_key_len = data_len;
  775. sm->EAPOLKeyReceived = TRUE;
  776. sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
  777. sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
  778. os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
  779. wpa_sm_step(sm);
  780. }
  781. static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
  782. u8 *gtk, size_t gtk_len)
  783. {
  784. u8 data[ETH_ALEN + WPA_NONCE_LEN];
  785. /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
  786. os_memcpy(data, addr, ETH_ALEN);
  787. os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
  788. #ifdef CONFIG_IEEE80211W
  789. sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
  790. data, sizeof(data), gtk, gtk_len);
  791. #else /* CONFIG_IEEE80211W */
  792. sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
  793. data, sizeof(data), gtk, gtk_len);
  794. #endif /* CONFIG_IEEE80211W */
  795. wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
  796. wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
  797. }
  798. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
  799. {
  800. struct wpa_authenticator *wpa_auth = eloop_ctx;
  801. struct wpa_state_machine *sm = timeout_ctx;
  802. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
  803. sm->TimeoutEvt = TRUE;
  804. wpa_sm_step(sm);
  805. }
  806. void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  807. struct wpa_state_machine *sm, int key_info,
  808. const u8 *key_rsc, const u8 *nonce,
  809. const u8 *kde, size_t kde_len,
  810. int keyidx, int encr, int force_version)
  811. {
  812. struct ieee802_1x_hdr *hdr;
  813. struct wpa_eapol_key *key;
  814. size_t len;
  815. int alg;
  816. int key_data_len, pad_len = 0;
  817. u8 *buf, *pos;
  818. int version, pairwise;
  819. int i;
  820. len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
  821. if (force_version)
  822. version = force_version;
  823. else if (wpa_use_aes_cmac(sm))
  824. version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
  825. else if (sm->pairwise == WPA_CIPHER_CCMP)
  826. version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
  827. else
  828. version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
  829. pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  830. wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
  831. "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
  832. "encr=%d)",
  833. version,
  834. (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
  835. (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
  836. (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
  837. (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
  838. pairwise, (unsigned long) kde_len, keyidx, encr);
  839. key_data_len = kde_len;
  840. if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  841. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
  842. pad_len = key_data_len % 8;
  843. if (pad_len)
  844. pad_len = 8 - pad_len;
  845. key_data_len += pad_len + 8;
  846. }
  847. len += key_data_len;
  848. hdr = os_zalloc(len);
  849. if (hdr == NULL)
  850. return;
  851. hdr->version = wpa_auth->conf.eapol_version;
  852. hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
  853. hdr->length = host_to_be16(len - sizeof(*hdr));
  854. key = (struct wpa_eapol_key *) (hdr + 1);
  855. key->type = sm->wpa == WPA_VERSION_WPA2 ?
  856. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  857. key_info |= version;
  858. if (encr && sm->wpa == WPA_VERSION_WPA2)
  859. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  860. if (sm->wpa != WPA_VERSION_WPA2)
  861. key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
  862. WPA_PUT_BE16(key->key_info, key_info);
  863. alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
  864. switch (alg) {
  865. case WPA_CIPHER_CCMP:
  866. WPA_PUT_BE16(key->key_length, 16);
  867. break;
  868. case WPA_CIPHER_TKIP:
  869. WPA_PUT_BE16(key->key_length, 32);
  870. break;
  871. case WPA_CIPHER_WEP40:
  872. WPA_PUT_BE16(key->key_length, 5);
  873. break;
  874. case WPA_CIPHER_WEP104:
  875. WPA_PUT_BE16(key->key_length, 13);
  876. break;
  877. }
  878. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  879. WPA_PUT_BE16(key->key_length, 0);
  880. /* FIX: STSL: what to use as key_replay_counter? */
  881. for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
  882. sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
  883. os_memcpy(sm->key_replay[i].counter,
  884. sm->key_replay[i - 1].counter,
  885. WPA_REPLAY_COUNTER_LEN);
  886. }
  887. inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
  888. os_memcpy(key->replay_counter, sm->key_replay[0].counter,
  889. WPA_REPLAY_COUNTER_LEN);
  890. sm->key_replay[0].valid = TRUE;
  891. if (nonce)
  892. os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
  893. if (key_rsc)
  894. os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
  895. if (kde && !encr) {
  896. os_memcpy(key + 1, kde, kde_len);
  897. WPA_PUT_BE16(key->key_data_length, kde_len);
  898. } else if (encr && kde) {
  899. buf = os_zalloc(key_data_len);
  900. if (buf == NULL) {
  901. os_free(hdr);
  902. return;
  903. }
  904. pos = buf;
  905. os_memcpy(pos, kde, kde_len);
  906. pos += kde_len;
  907. if (pad_len)
  908. *pos++ = 0xdd;
  909. wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
  910. buf, key_data_len);
  911. if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  912. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  913. if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
  914. (u8 *) (key + 1))) {
  915. os_free(hdr);
  916. os_free(buf);
  917. return;
  918. }
  919. WPA_PUT_BE16(key->key_data_length, key_data_len);
  920. } else {
  921. u8 ek[32];
  922. os_memcpy(key->key_iv,
  923. sm->group->Counter + WPA_NONCE_LEN - 16, 16);
  924. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  925. os_memcpy(ek, key->key_iv, 16);
  926. os_memcpy(ek + 16, sm->PTK.kek, 16);
  927. os_memcpy(key + 1, buf, key_data_len);
  928. rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
  929. WPA_PUT_BE16(key->key_data_length, key_data_len);
  930. }
  931. os_free(buf);
  932. }
  933. if (key_info & WPA_KEY_INFO_MIC) {
  934. if (!sm->PTK_valid) {
  935. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  936. "PTK not valid when sending EAPOL-Key "
  937. "frame");
  938. os_free(hdr);
  939. return;
  940. }
  941. wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
  942. key->key_mic);
  943. }
  944. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
  945. 1);
  946. wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
  947. sm->pairwise_set);
  948. os_free(hdr);
  949. }
  950. static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  951. struct wpa_state_machine *sm, int key_info,
  952. const u8 *key_rsc, const u8 *nonce,
  953. const u8 *kde, size_t kde_len,
  954. int keyidx, int encr)
  955. {
  956. int timeout_ms;
  957. int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  958. int ctr;
  959. if (sm == NULL)
  960. return;
  961. __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
  962. keyidx, encr, 0);
  963. ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
  964. if (ctr == 1)
  965. timeout_ms = eapol_key_timeout_first;
  966. else
  967. timeout_ms = eapol_key_timeout_subseq;
  968. eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
  969. wpa_send_eapol_timeout, wpa_auth, sm);
  970. }
  971. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
  972. {
  973. struct ieee802_1x_hdr *hdr;
  974. struct wpa_eapol_key *key;
  975. u16 key_info;
  976. int ret = 0;
  977. u8 mic[16];
  978. if (data_len < sizeof(*hdr) + sizeof(*key))
  979. return -1;
  980. hdr = (struct ieee802_1x_hdr *) data;
  981. key = (struct wpa_eapol_key *) (hdr + 1);
  982. key_info = WPA_GET_BE16(key->key_info);
  983. os_memcpy(mic, key->key_mic, 16);
  984. os_memset(key->key_mic, 0, 16);
  985. if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  986. data, data_len, key->key_mic) ||
  987. os_memcmp(mic, key->key_mic, 16) != 0)
  988. ret = -1;
  989. os_memcpy(key->key_mic, mic, 16);
  990. return ret;
  991. }
  992. void wpa_remove_ptk(struct wpa_state_machine *sm)
  993. {
  994. sm->PTK_valid = FALSE;
  995. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  996. wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, (u8 *) "",
  997. 0);
  998. sm->pairwise_set = FALSE;
  999. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  1000. }
  1001. void wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
  1002. {
  1003. int remove_ptk = 1;
  1004. if (sm == NULL)
  1005. return;
  1006. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1007. "event %d notification", event);
  1008. switch (event) {
  1009. case WPA_AUTH:
  1010. case WPA_ASSOC:
  1011. break;
  1012. case WPA_DEAUTH:
  1013. case WPA_DISASSOC:
  1014. sm->DeauthenticationRequest = TRUE;
  1015. break;
  1016. case WPA_REAUTH:
  1017. case WPA_REAUTH_EAPOL:
  1018. if (sm->GUpdateStationKeys) {
  1019. /*
  1020. * Reauthentication cancels the pending group key
  1021. * update for this STA.
  1022. */
  1023. sm->group->GKeyDoneStations--;
  1024. sm->GUpdateStationKeys = FALSE;
  1025. sm->PtkGroupInit = TRUE;
  1026. }
  1027. sm->ReAuthenticationRequest = TRUE;
  1028. break;
  1029. case WPA_ASSOC_FT:
  1030. #ifdef CONFIG_IEEE80211R
  1031. /* Using FT protocol, not WPA auth state machine */
  1032. sm->ft_completed = 1;
  1033. return;
  1034. #else /* CONFIG_IEEE80211R */
  1035. break;
  1036. #endif /* CONFIG_IEEE80211R */
  1037. }
  1038. #ifdef CONFIG_IEEE80211R
  1039. sm->ft_completed = 0;
  1040. #endif /* CONFIG_IEEE80211R */
  1041. #ifdef CONFIG_IEEE80211W
  1042. if (sm->mgmt_frame_prot && event == WPA_AUTH)
  1043. remove_ptk = 0;
  1044. #endif /* CONFIG_IEEE80211W */
  1045. if (remove_ptk) {
  1046. sm->PTK_valid = FALSE;
  1047. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1048. if (event != WPA_REAUTH_EAPOL)
  1049. wpa_remove_ptk(sm);
  1050. }
  1051. wpa_sm_step(sm);
  1052. }
  1053. static enum wpa_alg wpa_alg_enum(int alg)
  1054. {
  1055. switch (alg) {
  1056. case WPA_CIPHER_CCMP:
  1057. return WPA_ALG_CCMP;
  1058. case WPA_CIPHER_TKIP:
  1059. return WPA_ALG_TKIP;
  1060. case WPA_CIPHER_WEP104:
  1061. case WPA_CIPHER_WEP40:
  1062. return WPA_ALG_WEP;
  1063. default:
  1064. return WPA_ALG_NONE;
  1065. }
  1066. }
  1067. SM_STATE(WPA_PTK, INITIALIZE)
  1068. {
  1069. SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
  1070. if (sm->Init) {
  1071. /* Init flag is not cleared here, so avoid busy
  1072. * loop by claiming nothing changed. */
  1073. sm->changed = FALSE;
  1074. }
  1075. sm->keycount = 0;
  1076. if (sm->GUpdateStationKeys)
  1077. sm->group->GKeyDoneStations--;
  1078. sm->GUpdateStationKeys = FALSE;
  1079. if (sm->wpa == WPA_VERSION_WPA)
  1080. sm->PInitAKeys = FALSE;
  1081. if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
  1082. * Local AA > Remote AA)) */) {
  1083. sm->Pair = TRUE;
  1084. }
  1085. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
  1086. wpa_remove_ptk(sm);
  1087. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
  1088. sm->TimeoutCtr = 0;
  1089. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1090. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1091. WPA_EAPOL_authorized, 0);
  1092. }
  1093. }
  1094. SM_STATE(WPA_PTK, DISCONNECT)
  1095. {
  1096. SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
  1097. sm->Disconnect = FALSE;
  1098. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1099. }
  1100. SM_STATE(WPA_PTK, DISCONNECTED)
  1101. {
  1102. SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
  1103. sm->DeauthenticationRequest = FALSE;
  1104. }
  1105. SM_STATE(WPA_PTK, AUTHENTICATION)
  1106. {
  1107. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
  1108. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1109. sm->PTK_valid = FALSE;
  1110. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
  1111. 1);
  1112. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
  1113. sm->AuthenticationRequest = FALSE;
  1114. }
  1115. SM_STATE(WPA_PTK, AUTHENTICATION2)
  1116. {
  1117. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
  1118. os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
  1119. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  1120. sm->ReAuthenticationRequest = FALSE;
  1121. /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
  1122. * logical place than INITIALIZE since AUTHENTICATION2 can be
  1123. * re-entered on ReAuthenticationRequest without going through
  1124. * INITIALIZE. */
  1125. sm->TimeoutCtr = 0;
  1126. }
  1127. SM_STATE(WPA_PTK, INITPMK)
  1128. {
  1129. u8 msk[2 * PMK_LEN];
  1130. size_t len = 2 * PMK_LEN;
  1131. SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
  1132. #ifdef CONFIG_IEEE80211R
  1133. sm->xxkey_len = 0;
  1134. #endif /* CONFIG_IEEE80211R */
  1135. if (sm->pmksa) {
  1136. wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
  1137. os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
  1138. } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
  1139. wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
  1140. "(len=%lu)", (unsigned long) len);
  1141. os_memcpy(sm->PMK, msk, PMK_LEN);
  1142. #ifdef CONFIG_IEEE80211R
  1143. if (len >= 2 * PMK_LEN) {
  1144. os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
  1145. sm->xxkey_len = PMK_LEN;
  1146. }
  1147. #endif /* CONFIG_IEEE80211R */
  1148. } else {
  1149. wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
  1150. }
  1151. sm->req_replay_counter_used = 0;
  1152. /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
  1153. * will break reauthentication since EAPOL state machines may not be
  1154. * get into AUTHENTICATING state that clears keyRun before WPA state
  1155. * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
  1156. * state and takes PMK from the previously used AAA Key. This will
  1157. * eventually fail in 4-Way Handshake because Supplicant uses PMK
  1158. * derived from the new AAA Key. Setting keyRun = FALSE here seems to
  1159. * be good workaround for this issue. */
  1160. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
  1161. }
  1162. SM_STATE(WPA_PTK, INITPSK)
  1163. {
  1164. const u8 *psk;
  1165. SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
  1166. psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
  1167. if (psk) {
  1168. os_memcpy(sm->PMK, psk, PMK_LEN);
  1169. #ifdef CONFIG_IEEE80211R
  1170. os_memcpy(sm->xxkey, psk, PMK_LEN);
  1171. sm->xxkey_len = PMK_LEN;
  1172. #endif /* CONFIG_IEEE80211R */
  1173. }
  1174. sm->req_replay_counter_used = 0;
  1175. }
  1176. SM_STATE(WPA_PTK, PTKSTART)
  1177. {
  1178. u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
  1179. size_t pmkid_len = 0;
  1180. SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
  1181. sm->PTKRequest = FALSE;
  1182. sm->TimeoutEvt = FALSE;
  1183. sm->TimeoutCtr++;
  1184. if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
  1185. /* No point in sending the EAPOL-Key - we will disconnect
  1186. * immediately following this. */
  1187. return;
  1188. }
  1189. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1190. "sending 1/4 msg of 4-Way Handshake");
  1191. /*
  1192. * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
  1193. * one possible PSK for this STA.
  1194. */
  1195. if (sm->wpa == WPA_VERSION_WPA2 &&
  1196. wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
  1197. pmkid = buf;
  1198. pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
  1199. pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
  1200. pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
  1201. RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
  1202. if (sm->pmksa)
  1203. os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
  1204. sm->pmksa->pmkid, PMKID_LEN);
  1205. else {
  1206. /*
  1207. * Calculate PMKID since no PMKSA cache entry was
  1208. * available with pre-calculated PMKID.
  1209. */
  1210. rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
  1211. sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
  1212. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1213. }
  1214. }
  1215. wpa_send_eapol(sm->wpa_auth, sm,
  1216. WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
  1217. sm->ANonce, pmkid, pmkid_len, 0, 0);
  1218. }
  1219. static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
  1220. struct wpa_ptk *ptk)
  1221. {
  1222. size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
  1223. #ifdef CONFIG_IEEE80211R
  1224. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  1225. return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
  1226. #endif /* CONFIG_IEEE80211R */
  1227. wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
  1228. sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
  1229. (u8 *) ptk, ptk_len,
  1230. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1231. return 0;
  1232. }
  1233. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
  1234. {
  1235. struct wpa_ptk PTK;
  1236. int ok = 0;
  1237. const u8 *pmk = NULL;
  1238. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
  1239. sm->EAPOLKeyReceived = FALSE;
  1240. /* WPA with IEEE 802.1X: use the derived PMK from EAP
  1241. * WPA-PSK: iterate through possible PSKs and select the one matching
  1242. * the packet */
  1243. for (;;) {
  1244. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1245. pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
  1246. if (pmk == NULL)
  1247. break;
  1248. } else
  1249. pmk = sm->PMK;
  1250. wpa_derive_ptk(sm, pmk, &PTK);
  1251. if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
  1252. sm->last_rx_eapol_key_len) == 0) {
  1253. ok = 1;
  1254. break;
  1255. }
  1256. if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
  1257. break;
  1258. }
  1259. if (!ok) {
  1260. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1261. "invalid MIC in msg 2/4 of 4-Way Handshake");
  1262. return;
  1263. }
  1264. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  1265. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1266. /* PSK may have changed from the previous choice, so update
  1267. * state machine data based on whatever PSK was selected here.
  1268. */
  1269. os_memcpy(sm->PMK, pmk, PMK_LEN);
  1270. }
  1271. sm->MICVerified = TRUE;
  1272. os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
  1273. sm->PTK_valid = TRUE;
  1274. }
  1275. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
  1276. {
  1277. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
  1278. sm->TimeoutCtr = 0;
  1279. }
  1280. #ifdef CONFIG_IEEE80211W
  1281. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1282. {
  1283. if (sm->mgmt_frame_prot) {
  1284. return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
  1285. }
  1286. return 0;
  1287. }
  1288. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1289. {
  1290. struct wpa_igtk_kde igtk;
  1291. struct wpa_group *gsm = sm->group;
  1292. if (!sm->mgmt_frame_prot)
  1293. return pos;
  1294. igtk.keyid[0] = gsm->GN_igtk;
  1295. igtk.keyid[1] = 0;
  1296. if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) < 0)
  1297. os_memset(igtk.pn, 0, sizeof(igtk.pn));
  1298. os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
  1299. pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
  1300. (const u8 *) &igtk, sizeof(igtk), NULL, 0);
  1301. return pos;
  1302. }
  1303. #else /* CONFIG_IEEE80211W */
  1304. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1305. {
  1306. return 0;
  1307. }
  1308. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1309. {
  1310. return pos;
  1311. }
  1312. #endif /* CONFIG_IEEE80211W */
  1313. SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
  1314. {
  1315. u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
  1316. size_t gtk_len, kde_len;
  1317. struct wpa_group *gsm = sm->group;
  1318. u8 *wpa_ie;
  1319. int wpa_ie_len, secure, keyidx, encr = 0;
  1320. SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
  1321. sm->TimeoutEvt = FALSE;
  1322. sm->TimeoutCtr++;
  1323. if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
  1324. /* No point in sending the EAPOL-Key - we will disconnect
  1325. * immediately following this. */
  1326. return;
  1327. }
  1328. /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
  1329. */
  1330. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1331. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1332. wpa_ie = sm->wpa_auth->wpa_ie;
  1333. wpa_ie_len = sm->wpa_auth->wpa_ie_len;
  1334. if (sm->wpa == WPA_VERSION_WPA &&
  1335. (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
  1336. wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
  1337. /* WPA-only STA, remove RSN IE */
  1338. wpa_ie = wpa_ie + wpa_ie[1] + 2;
  1339. wpa_ie_len = wpa_ie[1] + 2;
  1340. }
  1341. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1342. "sending 3/4 msg of 4-Way Handshake");
  1343. if (sm->wpa == WPA_VERSION_WPA2) {
  1344. /* WPA2 send GTK in the 4-way handshake */
  1345. secure = 1;
  1346. gtk = gsm->GTK[gsm->GN - 1];
  1347. gtk_len = gsm->GTK_len;
  1348. keyidx = gsm->GN;
  1349. _rsc = rsc;
  1350. encr = 1;
  1351. } else {
  1352. /* WPA does not include GTK in msg 3/4 */
  1353. secure = 0;
  1354. gtk = NULL;
  1355. gtk_len = 0;
  1356. keyidx = 0;
  1357. _rsc = NULL;
  1358. }
  1359. kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
  1360. if (gtk)
  1361. kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
  1362. kde = os_malloc(kde_len);
  1363. if (kde == NULL)
  1364. return;
  1365. pos = kde;
  1366. os_memcpy(pos, wpa_ie, wpa_ie_len);
  1367. pos += wpa_ie_len;
  1368. if (gtk) {
  1369. u8 hdr[2];
  1370. hdr[0] = keyidx & 0x03;
  1371. hdr[1] = 0;
  1372. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1373. gtk, gtk_len);
  1374. }
  1375. pos = ieee80211w_kde_add(sm, pos);
  1376. wpa_send_eapol(sm->wpa_auth, sm,
  1377. (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
  1378. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
  1379. WPA_KEY_INFO_KEY_TYPE,
  1380. _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
  1381. os_free(kde);
  1382. }
  1383. SM_STATE(WPA_PTK, PTKINITDONE)
  1384. {
  1385. SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
  1386. sm->EAPOLKeyReceived = FALSE;
  1387. if (sm->Pair) {
  1388. enum wpa_alg alg;
  1389. int klen;
  1390. if (sm->pairwise == WPA_CIPHER_TKIP) {
  1391. alg = WPA_ALG_TKIP;
  1392. klen = 32;
  1393. } else {
  1394. alg = WPA_ALG_CCMP;
  1395. klen = 16;
  1396. }
  1397. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  1398. sm->PTK.tk1, klen)) {
  1399. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1400. return;
  1401. }
  1402. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  1403. sm->pairwise_set = TRUE;
  1404. if (sm->wpa_auth->conf.wpa_ptk_rekey) {
  1405. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  1406. eloop_register_timeout(sm->wpa_auth->conf.
  1407. wpa_ptk_rekey, 0, wpa_rekey_ptk,
  1408. sm->wpa_auth, sm);
  1409. }
  1410. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1411. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1412. WPA_EAPOL_authorized, 1);
  1413. }
  1414. }
  1415. if (0 /* IBSS == TRUE */) {
  1416. sm->keycount++;
  1417. if (sm->keycount == 2) {
  1418. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1419. WPA_EAPOL_portValid, 1);
  1420. }
  1421. } else {
  1422. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
  1423. 1);
  1424. }
  1425. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
  1426. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
  1427. if (sm->wpa == WPA_VERSION_WPA)
  1428. sm->PInitAKeys = TRUE;
  1429. else
  1430. sm->has_GTK = TRUE;
  1431. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1432. "pairwise key handshake completed (%s)",
  1433. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1434. #ifdef CONFIG_IEEE80211R
  1435. wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
  1436. #endif /* CONFIG_IEEE80211R */
  1437. }
  1438. SM_STEP(WPA_PTK)
  1439. {
  1440. struct wpa_authenticator *wpa_auth = sm->wpa_auth;
  1441. if (sm->Init)
  1442. SM_ENTER(WPA_PTK, INITIALIZE);
  1443. else if (sm->Disconnect
  1444. /* || FIX: dot11RSNAConfigSALifetime timeout */)
  1445. SM_ENTER(WPA_PTK, DISCONNECT);
  1446. else if (sm->DeauthenticationRequest)
  1447. SM_ENTER(WPA_PTK, DISCONNECTED);
  1448. else if (sm->AuthenticationRequest)
  1449. SM_ENTER(WPA_PTK, AUTHENTICATION);
  1450. else if (sm->ReAuthenticationRequest)
  1451. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1452. else if (sm->PTKRequest)
  1453. SM_ENTER(WPA_PTK, PTKSTART);
  1454. else switch (sm->wpa_ptk_state) {
  1455. case WPA_PTK_INITIALIZE:
  1456. break;
  1457. case WPA_PTK_DISCONNECT:
  1458. SM_ENTER(WPA_PTK, DISCONNECTED);
  1459. break;
  1460. case WPA_PTK_DISCONNECTED:
  1461. SM_ENTER(WPA_PTK, INITIALIZE);
  1462. break;
  1463. case WPA_PTK_AUTHENTICATION:
  1464. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1465. break;
  1466. case WPA_PTK_AUTHENTICATION2:
  1467. if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  1468. wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1469. WPA_EAPOL_keyRun) > 0)
  1470. SM_ENTER(WPA_PTK, INITPMK);
  1471. else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
  1472. /* FIX: && 802.1X::keyRun */)
  1473. SM_ENTER(WPA_PTK, INITPSK);
  1474. break;
  1475. case WPA_PTK_INITPMK:
  1476. if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1477. WPA_EAPOL_keyAvailable) > 0)
  1478. SM_ENTER(WPA_PTK, PTKSTART);
  1479. else {
  1480. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1481. SM_ENTER(WPA_PTK, DISCONNECT);
  1482. }
  1483. break;
  1484. case WPA_PTK_INITPSK:
  1485. if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
  1486. SM_ENTER(WPA_PTK, PTKSTART);
  1487. else {
  1488. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1489. "no PSK configured for the STA");
  1490. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1491. SM_ENTER(WPA_PTK, DISCONNECT);
  1492. }
  1493. break;
  1494. case WPA_PTK_PTKSTART:
  1495. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1496. sm->EAPOLKeyPairwise)
  1497. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1498. else if (sm->TimeoutCtr >
  1499. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1500. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1501. SM_ENTER(WPA_PTK, DISCONNECT);
  1502. } else if (sm->TimeoutEvt)
  1503. SM_ENTER(WPA_PTK, PTKSTART);
  1504. break;
  1505. case WPA_PTK_PTKCALCNEGOTIATING:
  1506. if (sm->MICVerified)
  1507. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
  1508. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1509. sm->EAPOLKeyPairwise)
  1510. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1511. else if (sm->TimeoutEvt)
  1512. SM_ENTER(WPA_PTK, PTKSTART);
  1513. break;
  1514. case WPA_PTK_PTKCALCNEGOTIATING2:
  1515. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1516. break;
  1517. case WPA_PTK_PTKINITNEGOTIATING:
  1518. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1519. sm->EAPOLKeyPairwise && sm->MICVerified)
  1520. SM_ENTER(WPA_PTK, PTKINITDONE);
  1521. else if (sm->TimeoutCtr >
  1522. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1523. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1524. SM_ENTER(WPA_PTK, DISCONNECT);
  1525. } else if (sm->TimeoutEvt)
  1526. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1527. break;
  1528. case WPA_PTK_PTKINITDONE:
  1529. break;
  1530. }
  1531. }
  1532. SM_STATE(WPA_PTK_GROUP, IDLE)
  1533. {
  1534. SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
  1535. if (sm->Init) {
  1536. /* Init flag is not cleared here, so avoid busy
  1537. * loop by claiming nothing changed. */
  1538. sm->changed = FALSE;
  1539. }
  1540. sm->GTimeoutCtr = 0;
  1541. }
  1542. SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
  1543. {
  1544. u8 rsc[WPA_KEY_RSC_LEN];
  1545. struct wpa_group *gsm = sm->group;
  1546. u8 *kde, *pos, hdr[2];
  1547. size_t kde_len;
  1548. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
  1549. sm->GTimeoutCtr++;
  1550. if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
  1551. /* No point in sending the EAPOL-Key - we will disconnect
  1552. * immediately following this. */
  1553. return;
  1554. }
  1555. if (sm->wpa == WPA_VERSION_WPA)
  1556. sm->PInitAKeys = FALSE;
  1557. sm->TimeoutEvt = FALSE;
  1558. /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
  1559. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1560. if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
  1561. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1562. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1563. "sending 1/2 msg of Group Key Handshake");
  1564. if (sm->wpa == WPA_VERSION_WPA2) {
  1565. kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
  1566. ieee80211w_kde_len(sm);
  1567. kde = os_malloc(kde_len);
  1568. if (kde == NULL)
  1569. return;
  1570. pos = kde;
  1571. hdr[0] = gsm->GN & 0x03;
  1572. hdr[1] = 0;
  1573. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1574. gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  1575. pos = ieee80211w_kde_add(sm, pos);
  1576. } else {
  1577. kde = gsm->GTK[gsm->GN - 1];
  1578. pos = kde + gsm->GTK_len;
  1579. }
  1580. wpa_send_eapol(sm->wpa_auth, sm,
  1581. WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  1582. WPA_KEY_INFO_ACK |
  1583. (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
  1584. rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
  1585. if (sm->wpa == WPA_VERSION_WPA2)
  1586. os_free(kde);
  1587. }
  1588. SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
  1589. {
  1590. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
  1591. sm->EAPOLKeyReceived = FALSE;
  1592. if (sm->GUpdateStationKeys)
  1593. sm->group->GKeyDoneStations--;
  1594. sm->GUpdateStationKeys = FALSE;
  1595. sm->GTimeoutCtr = 0;
  1596. /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
  1597. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1598. "group key handshake completed (%s)",
  1599. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1600. sm->has_GTK = TRUE;
  1601. }
  1602. SM_STATE(WPA_PTK_GROUP, KEYERROR)
  1603. {
  1604. SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
  1605. if (sm->GUpdateStationKeys)
  1606. sm->group->GKeyDoneStations--;
  1607. sm->GUpdateStationKeys = FALSE;
  1608. sm->Disconnect = TRUE;
  1609. }
  1610. SM_STEP(WPA_PTK_GROUP)
  1611. {
  1612. if (sm->Init || sm->PtkGroupInit) {
  1613. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1614. sm->PtkGroupInit = FALSE;
  1615. } else switch (sm->wpa_ptk_group_state) {
  1616. case WPA_PTK_GROUP_IDLE:
  1617. if (sm->GUpdateStationKeys ||
  1618. (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
  1619. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  1620. break;
  1621. case WPA_PTK_GROUP_REKEYNEGOTIATING:
  1622. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1623. !sm->EAPOLKeyPairwise && sm->MICVerified)
  1624. SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
  1625. else if (sm->GTimeoutCtr >
  1626. (int) dot11RSNAConfigGroupUpdateCount)
  1627. SM_ENTER(WPA_PTK_GROUP, KEYERROR);
  1628. else if (sm->TimeoutEvt)
  1629. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  1630. break;
  1631. case WPA_PTK_GROUP_KEYERROR:
  1632. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1633. break;
  1634. case WPA_PTK_GROUP_REKEYESTABLISHED:
  1635. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1636. break;
  1637. }
  1638. }
  1639. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  1640. struct wpa_group *group)
  1641. {
  1642. int ret = 0;
  1643. /* FIX: is this the correct way of getting GNonce? */
  1644. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  1645. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  1646. wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
  1647. group->GTK[group->GN - 1], group->GTK_len);
  1648. #ifdef CONFIG_IEEE80211W
  1649. if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  1650. if (os_get_random(group->IGTK[group->GN_igtk - 4],
  1651. WPA_IGTK_LEN) < 0) {
  1652. wpa_printf(MSG_INFO, "RSN: Failed to get new random "
  1653. "IGTK");
  1654. ret = -1;
  1655. }
  1656. wpa_hexdump_key(MSG_DEBUG, "IGTK",
  1657. group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
  1658. }
  1659. #endif /* CONFIG_IEEE80211W */
  1660. return ret;
  1661. }
  1662. static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
  1663. struct wpa_group *group)
  1664. {
  1665. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1666. "GTK_INIT (VLAN-ID %d)", group->vlan_id);
  1667. group->changed = FALSE; /* GInit is not cleared here; avoid loop */
  1668. group->wpa_group_state = WPA_GROUP_GTK_INIT;
  1669. /* GTK[0..N] = 0 */
  1670. os_memset(group->GTK, 0, sizeof(group->GTK));
  1671. group->GN = 1;
  1672. group->GM = 2;
  1673. #ifdef CONFIG_IEEE80211W
  1674. group->GN_igtk = 4;
  1675. group->GM_igtk = 5;
  1676. #endif /* CONFIG_IEEE80211W */
  1677. /* GTK[GN] = CalcGTK() */
  1678. wpa_gtk_update(wpa_auth, group);
  1679. }
  1680. static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
  1681. {
  1682. if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
  1683. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1684. "Not in PTKINITDONE; skip Group Key update");
  1685. return 0;
  1686. }
  1687. if (sm->GUpdateStationKeys) {
  1688. /*
  1689. * This should not really happen, but just in case, make sure
  1690. * we do not count the same STA twice in GKeyDoneStations.
  1691. */
  1692. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1693. "GUpdateStationKeys already set - do not "
  1694. "increment GKeyDoneStations");
  1695. } else {
  1696. sm->group->GKeyDoneStations++;
  1697. sm->GUpdateStationKeys = TRUE;
  1698. }
  1699. wpa_sm_step(sm);
  1700. return 0;
  1701. }
  1702. static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
  1703. struct wpa_group *group)
  1704. {
  1705. int tmp;
  1706. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1707. "SETKEYS (VLAN-ID %d)", group->vlan_id);
  1708. group->changed = TRUE;
  1709. group->wpa_group_state = WPA_GROUP_SETKEYS;
  1710. group->GTKReKey = FALSE;
  1711. tmp = group->GM;
  1712. group->GM = group->GN;
  1713. group->GN = tmp;
  1714. #ifdef CONFIG_IEEE80211W
  1715. tmp = group->GM_igtk;
  1716. group->GM_igtk = group->GN_igtk;
  1717. group->GN_igtk = tmp;
  1718. #endif /* CONFIG_IEEE80211W */
  1719. /* "GKeyDoneStations = GNoStations" is done in more robust way by
  1720. * counting the STAs that are marked with GUpdateStationKeys instead of
  1721. * including all STAs that could be in not-yet-completed state. */
  1722. wpa_gtk_update(wpa_auth, group);
  1723. wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
  1724. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
  1725. group->GKeyDoneStations);
  1726. }
  1727. static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
  1728. struct wpa_group *group)
  1729. {
  1730. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1731. "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
  1732. group->changed = TRUE;
  1733. group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
  1734. wpa_auth_set_key(wpa_auth, group->vlan_id,
  1735. wpa_alg_enum(wpa_auth->conf.wpa_group),
  1736. NULL, group->GN, group->GTK[group->GN - 1],
  1737. group->GTK_len);
  1738. #ifdef CONFIG_IEEE80211W
  1739. if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  1740. wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
  1741. NULL, group->GN_igtk,
  1742. group->IGTK[group->GN_igtk - 4],
  1743. WPA_IGTK_LEN);
  1744. }
  1745. #endif /* CONFIG_IEEE80211W */
  1746. }
  1747. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  1748. struct wpa_group *group)
  1749. {
  1750. if (group->GInit) {
  1751. wpa_group_gtk_init(wpa_auth, group);
  1752. } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
  1753. group->GTKAuthenticator) {
  1754. wpa_group_setkeysdone(wpa_auth, group);
  1755. } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
  1756. group->GTKReKey) {
  1757. wpa_group_setkeys(wpa_auth, group);
  1758. } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
  1759. if (group->GKeyDoneStations == 0)
  1760. wpa_group_setkeysdone(wpa_auth, group);
  1761. else if (group->GTKReKey)
  1762. wpa_group_setkeys(wpa_auth, group);
  1763. }
  1764. }
  1765. static void wpa_sm_step(struct wpa_state_machine *sm)
  1766. {
  1767. if (sm == NULL)
  1768. return;
  1769. if (sm->in_step_loop) {
  1770. /* This should not happen, but if it does, make sure we do not
  1771. * end up freeing the state machine too early by exiting the
  1772. * recursive call. */
  1773. wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
  1774. return;
  1775. }
  1776. sm->in_step_loop = 1;
  1777. do {
  1778. if (sm->pending_deinit)
  1779. break;
  1780. sm->changed = FALSE;
  1781. sm->wpa_auth->group->changed = FALSE;
  1782. SM_STEP_RUN(WPA_PTK);
  1783. if (sm->pending_deinit)
  1784. break;
  1785. SM_STEP_RUN(WPA_PTK_GROUP);
  1786. if (sm->pending_deinit)
  1787. break;
  1788. wpa_group_sm_step(sm->wpa_auth, sm->group);
  1789. } while (sm->changed || sm->wpa_auth->group->changed);
  1790. sm->in_step_loop = 0;
  1791. if (sm->pending_deinit) {
  1792. wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
  1793. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  1794. wpa_free_sta_sm(sm);
  1795. }
  1796. }
  1797. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
  1798. {
  1799. struct wpa_state_machine *sm = eloop_ctx;
  1800. wpa_sm_step(sm);
  1801. }
  1802. void wpa_auth_sm_notify(struct wpa_state_machine *sm)
  1803. {
  1804. if (sm == NULL)
  1805. return;
  1806. eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
  1807. }
  1808. void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
  1809. {
  1810. int tmp, i;
  1811. struct wpa_group *group;
  1812. if (wpa_auth == NULL)
  1813. return;
  1814. group = wpa_auth->group;
  1815. for (i = 0; i < 2; i++) {
  1816. tmp = group->GM;
  1817. group->GM = group->GN;
  1818. group->GN = tmp;
  1819. #ifdef CONFIG_IEEE80211W
  1820. tmp = group->GM_igtk;
  1821. group->GM_igtk = group->GN_igtk;
  1822. group->GN_igtk = tmp;
  1823. #endif /* CONFIG_IEEE80211W */
  1824. wpa_gtk_update(wpa_auth, group);
  1825. }
  1826. }
  1827. static const char * wpa_bool_txt(int bool)
  1828. {
  1829. return bool ? "TRUE" : "FALSE";
  1830. }
  1831. static int wpa_cipher_bits(int cipher)
  1832. {
  1833. switch (cipher) {
  1834. case WPA_CIPHER_CCMP:
  1835. return 128;
  1836. case WPA_CIPHER_TKIP:
  1837. return 256;
  1838. case WPA_CIPHER_WEP104:
  1839. return 104;
  1840. case WPA_CIPHER_WEP40:
  1841. return 40;
  1842. default:
  1843. return 0;
  1844. }
  1845. }
  1846. #define RSN_SUITE "%02x-%02x-%02x-%d"
  1847. #define RSN_SUITE_ARG(s) \
  1848. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  1849. int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
  1850. {
  1851. int len = 0, ret;
  1852. char pmkid_txt[PMKID_LEN * 2 + 1];
  1853. if (wpa_auth == NULL)
  1854. return len;
  1855. ret = os_snprintf(buf + len, buflen - len,
  1856. "dot11RSNAOptionImplemented=TRUE\n"
  1857. #ifdef CONFIG_RSN_PREAUTH
  1858. "dot11RSNAPreauthenticationImplemented=TRUE\n"
  1859. #else /* CONFIG_RSN_PREAUTH */
  1860. "dot11RSNAPreauthenticationImplemented=FALSE\n"
  1861. #endif /* CONFIG_RSN_PREAUTH */
  1862. "dot11RSNAEnabled=%s\n"
  1863. "dot11RSNAPreauthenticationEnabled=%s\n",
  1864. wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
  1865. wpa_bool_txt(wpa_auth->conf.rsn_preauth));
  1866. if (ret < 0 || (size_t) ret >= buflen - len)
  1867. return len;
  1868. len += ret;
  1869. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  1870. wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
  1871. ret = os_snprintf(
  1872. buf + len, buflen - len,
  1873. "dot11RSNAConfigVersion=%u\n"
  1874. "dot11RSNAConfigPairwiseKeysSupported=9999\n"
  1875. /* FIX: dot11RSNAConfigGroupCipher */
  1876. /* FIX: dot11RSNAConfigGroupRekeyMethod */
  1877. /* FIX: dot11RSNAConfigGroupRekeyTime */
  1878. /* FIX: dot11RSNAConfigGroupRekeyPackets */
  1879. "dot11RSNAConfigGroupRekeyStrict=%u\n"
  1880. "dot11RSNAConfigGroupUpdateCount=%u\n"
  1881. "dot11RSNAConfigPairwiseUpdateCount=%u\n"
  1882. "dot11RSNAConfigGroupCipherSize=%u\n"
  1883. "dot11RSNAConfigPMKLifetime=%u\n"
  1884. "dot11RSNAConfigPMKReauthThreshold=%u\n"
  1885. "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
  1886. "dot11RSNAConfigSATimeout=%u\n"
  1887. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  1888. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  1889. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  1890. "dot11RSNAPMKIDUsed=%s\n"
  1891. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  1892. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  1893. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  1894. "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
  1895. "dot11RSNA4WayHandshakeFailures=%u\n"
  1896. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
  1897. RSN_VERSION,
  1898. !!wpa_auth->conf.wpa_strict_rekey,
  1899. dot11RSNAConfigGroupUpdateCount,
  1900. dot11RSNAConfigPairwiseUpdateCount,
  1901. wpa_cipher_bits(wpa_auth->conf.wpa_group),
  1902. dot11RSNAConfigPMKLifetime,
  1903. dot11RSNAConfigPMKReauthThreshold,
  1904. dot11RSNAConfigSATimeout,
  1905. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
  1906. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
  1907. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
  1908. pmkid_txt,
  1909. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
  1910. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
  1911. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
  1912. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
  1913. wpa_auth->dot11RSNA4WayHandshakeFailures);
  1914. if (ret < 0 || (size_t) ret >= buflen - len)
  1915. return len;
  1916. len += ret;
  1917. /* TODO: dot11RSNAConfigPairwiseCiphersTable */
  1918. /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
  1919. /* Private MIB */
  1920. ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
  1921. wpa_auth->group->wpa_group_state);
  1922. if (ret < 0 || (size_t) ret >= buflen - len)
  1923. return len;
  1924. len += ret;
  1925. return len;
  1926. }
  1927. int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
  1928. {
  1929. int len = 0, ret;
  1930. u32 pairwise = 0;
  1931. if (sm == NULL)
  1932. return 0;
  1933. /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
  1934. /* dot11RSNAStatsEntry */
  1935. if (sm->wpa == WPA_VERSION_WPA) {
  1936. if (sm->pairwise == WPA_CIPHER_CCMP)
  1937. pairwise = WPA_CIPHER_SUITE_CCMP;
  1938. else if (sm->pairwise == WPA_CIPHER_TKIP)
  1939. pairwise = WPA_CIPHER_SUITE_TKIP;
  1940. else if (sm->pairwise == WPA_CIPHER_WEP104)
  1941. pairwise = WPA_CIPHER_SUITE_WEP104;
  1942. else if (sm->pairwise == WPA_CIPHER_WEP40)
  1943. pairwise = WPA_CIPHER_SUITE_WEP40;
  1944. else if (sm->pairwise == WPA_CIPHER_NONE)
  1945. pairwise = WPA_CIPHER_SUITE_NONE;
  1946. } else if (sm->wpa == WPA_VERSION_WPA2) {
  1947. if (sm->pairwise == WPA_CIPHER_CCMP)
  1948. pairwise = RSN_CIPHER_SUITE_CCMP;
  1949. else if (sm->pairwise == WPA_CIPHER_TKIP)
  1950. pairwise = RSN_CIPHER_SUITE_TKIP;
  1951. else if (sm->pairwise == WPA_CIPHER_WEP104)
  1952. pairwise = RSN_CIPHER_SUITE_WEP104;
  1953. else if (sm->pairwise == WPA_CIPHER_WEP40)
  1954. pairwise = RSN_CIPHER_SUITE_WEP40;
  1955. else if (sm->pairwise == WPA_CIPHER_NONE)
  1956. pairwise = RSN_CIPHER_SUITE_NONE;
  1957. } else
  1958. return 0;
  1959. ret = os_snprintf(
  1960. buf + len, buflen - len,
  1961. /* TODO: dot11RSNAStatsIndex */
  1962. "dot11RSNAStatsSTAAddress=" MACSTR "\n"
  1963. "dot11RSNAStatsVersion=1\n"
  1964. "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
  1965. /* TODO: dot11RSNAStatsTKIPICVErrors */
  1966. "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
  1967. "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
  1968. /* TODO: dot11RSNAStatsCCMPReplays */
  1969. /* TODO: dot11RSNAStatsCCMPDecryptErrors */
  1970. /* TODO: dot11RSNAStatsTKIPReplays */,
  1971. MAC2STR(sm->addr),
  1972. RSN_SUITE_ARG(pairwise),
  1973. sm->dot11RSNAStatsTKIPLocalMICFailures,
  1974. sm->dot11RSNAStatsTKIPRemoteMICFailures);
  1975. if (ret < 0 || (size_t) ret >= buflen - len)
  1976. return len;
  1977. len += ret;
  1978. /* Private MIB */
  1979. ret = os_snprintf(buf + len, buflen - len,
  1980. "hostapdWPAPTKState=%d\n"
  1981. "hostapdWPAPTKGroupState=%d\n",
  1982. sm->wpa_ptk_state,
  1983. sm->wpa_ptk_group_state);
  1984. if (ret < 0 || (size_t) ret >= buflen - len)
  1985. return len;
  1986. len += ret;
  1987. return len;
  1988. }
  1989. void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
  1990. {
  1991. if (wpa_auth)
  1992. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
  1993. }
  1994. int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
  1995. {
  1996. return sm && sm->pairwise_set;
  1997. }
  1998. int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
  1999. {
  2000. return sm->pairwise;
  2001. }
  2002. int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
  2003. {
  2004. if (sm == NULL)
  2005. return -1;
  2006. return sm->wpa_key_mgmt;
  2007. }
  2008. int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
  2009. {
  2010. if (sm == NULL)
  2011. return 0;
  2012. return sm->wpa;
  2013. }
  2014. int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
  2015. struct rsn_pmksa_cache_entry *entry)
  2016. {
  2017. if (sm == NULL || sm->pmksa != entry)
  2018. return -1;
  2019. sm->pmksa = NULL;
  2020. return 0;
  2021. }
  2022. struct rsn_pmksa_cache_entry *
  2023. wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
  2024. {
  2025. return sm ? sm->pmksa : NULL;
  2026. }
  2027. void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
  2028. {
  2029. if (sm)
  2030. sm->dot11RSNAStatsTKIPLocalMICFailures++;
  2031. }
  2032. const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
  2033. {
  2034. if (wpa_auth == NULL)
  2035. return NULL;
  2036. *len = wpa_auth->wpa_ie_len;
  2037. return wpa_auth->wpa_ie;
  2038. }
  2039. int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
  2040. int session_timeout, struct eapol_state_machine *eapol)
  2041. {
  2042. if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
  2043. return -1;
  2044. if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
  2045. sm->wpa_auth->addr, sm->addr, session_timeout,
  2046. eapol, sm->wpa_key_mgmt))
  2047. return 0;
  2048. return -1;
  2049. }
  2050. int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
  2051. const u8 *pmk, size_t len, const u8 *sta_addr,
  2052. int session_timeout,
  2053. struct eapol_state_machine *eapol)
  2054. {
  2055. if (wpa_auth == NULL)
  2056. return -1;
  2057. if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
  2058. sta_addr, session_timeout, eapol,
  2059. WPA_KEY_MGMT_IEEE8021X))
  2060. return 0;
  2061. return -1;
  2062. }
  2063. static struct wpa_group *
  2064. wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  2065. {
  2066. struct wpa_group *group;
  2067. if (wpa_auth == NULL || wpa_auth->group == NULL)
  2068. return NULL;
  2069. wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
  2070. vlan_id);
  2071. group = wpa_group_init(wpa_auth, vlan_id);
  2072. if (group == NULL)
  2073. return NULL;
  2074. group->next = wpa_auth->group->next;
  2075. wpa_auth->group->next = group;
  2076. return group;
  2077. }
  2078. int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
  2079. {
  2080. struct wpa_group *group;
  2081. if (sm == NULL || sm->wpa_auth == NULL)
  2082. return 0;
  2083. group = sm->wpa_auth->group;
  2084. while (group) {
  2085. if (group->vlan_id == vlan_id)
  2086. break;
  2087. group = group->next;
  2088. }
  2089. if (group == NULL) {
  2090. group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
  2091. if (group == NULL)
  2092. return -1;
  2093. }
  2094. if (sm->group == group)
  2095. return 0;
  2096. wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
  2097. "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
  2098. sm->group = group;
  2099. return 0;
  2100. }
  2101. #endif /* CONFIG_NATIVE_WINDOWS */