wpa.c 63 KB

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