wpa.c 61 KB

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