wpa.c 63 KB

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