wpa.c 62 KB

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