wpa.c 63 KB

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