wpa.c 63 KB

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