wpa.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  1. /*
  2. * hostapd - IEEE 802.11i-2004 / WPA Authenticator
  3. * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include "common.h"
  17. #include "config.h"
  18. #include "eapol_sm.h"
  19. #include "wpa.h"
  20. #include "sha1.h"
  21. #include "sha256.h"
  22. #include "rc4.h"
  23. #include "aes_wrap.h"
  24. #include "crypto.h"
  25. #include "eloop.h"
  26. #include "ieee802_11.h"
  27. #include "pmksa_cache.h"
  28. #include "state_machine.h"
  29. #include "wpa_auth_i.h"
  30. #include "wpa_auth_ie.h"
  31. #define STATE_MACHINE_DATA struct wpa_state_machine
  32. #define STATE_MACHINE_DEBUG_PREFIX "WPA"
  33. #define STATE_MACHINE_ADDR sm->addr
  34. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
  35. static void wpa_sm_step(struct wpa_state_machine *sm);
  36. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
  37. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
  38. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  39. struct wpa_group *group);
  40. static void wpa_request_new_ptk(struct wpa_state_machine *sm);
  41. /* Default timeouts are 100 ms, but this seems to be a bit too fast for most
  42. * WPA Supplicants, so use a bit longer timeout. */
  43. static const u32 dot11RSNAConfigGroupUpdateTimeOut = 1000; /* ms */
  44. static const u32 dot11RSNAConfigGroupUpdateCount = 3;
  45. static const u32 dot11RSNAConfigPairwiseUpdateTimeOut = 1000; /* ms */
  46. static const u32 dot11RSNAConfigPairwiseUpdateCount = 3;
  47. /* TODO: make these configurable */
  48. static const int dot11RSNAConfigPMKLifetime = 43200;
  49. static const int dot11RSNAConfigPMKReauthThreshold = 70;
  50. static const int dot11RSNAConfigSATimeout = 60;
  51. static inline void wpa_auth_mic_failure_report(
  52. struct wpa_authenticator *wpa_auth, const u8 *addr)
  53. {
  54. if (wpa_auth->cb.mic_failure_report)
  55. wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
  56. }
  57. static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
  58. const u8 *addr, wpa_eapol_variable var,
  59. int value)
  60. {
  61. if (wpa_auth->cb.set_eapol)
  62. wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
  63. }
  64. static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
  65. const u8 *addr, wpa_eapol_variable var)
  66. {
  67. if (wpa_auth->cb.get_eapol == NULL)
  68. return -1;
  69. return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
  70. }
  71. static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
  72. const u8 *addr, const u8 *prev_psk)
  73. {
  74. if (wpa_auth->cb.get_psk == NULL)
  75. return NULL;
  76. return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
  77. }
  78. static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
  79. const u8 *addr, u8 *msk, size_t *len)
  80. {
  81. if (wpa_auth->cb.get_msk == NULL)
  82. return -1;
  83. return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
  84. }
  85. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  86. int vlan_id,
  87. const char *alg, const u8 *addr, int idx,
  88. u8 *key, size_t key_len)
  89. {
  90. if (wpa_auth->cb.set_key == NULL)
  91. return -1;
  92. return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
  93. key, key_len);
  94. }
  95. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  96. const u8 *addr, int idx, u8 *seq)
  97. {
  98. if (wpa_auth->cb.get_seqnum == NULL)
  99. return -1;
  100. return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
  101. }
  102. static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth,
  103. const u8 *addr, int idx, u8 *seq)
  104. {
  105. if (wpa_auth->cb.get_seqnum_igtk == NULL)
  106. return -1;
  107. return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq);
  108. }
  109. static inline int
  110. wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
  111. const u8 *data, size_t data_len, int encrypt)
  112. {
  113. if (wpa_auth->cb.send_eapol == NULL)
  114. return -1;
  115. return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
  116. encrypt);
  117. }
  118. int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
  119. int (*cb)(struct wpa_state_machine *sm, void *ctx),
  120. void *cb_ctx)
  121. {
  122. if (wpa_auth->cb.for_each_sta == NULL)
  123. return 0;
  124. return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
  125. }
  126. int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
  127. int (*cb)(struct wpa_authenticator *a, void *ctx),
  128. void *cb_ctx)
  129. {
  130. if (wpa_auth->cb.for_each_auth == NULL)
  131. return 0;
  132. return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
  133. }
  134. void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  135. logger_level level, const char *txt)
  136. {
  137. if (wpa_auth->cb.logger == NULL)
  138. return;
  139. wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
  140. }
  141. void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  142. logger_level level, const char *fmt, ...)
  143. {
  144. char *format;
  145. int maxlen;
  146. va_list ap;
  147. if (wpa_auth->cb.logger == NULL)
  148. return;
  149. maxlen = os_strlen(fmt) + 100;
  150. format = os_malloc(maxlen);
  151. if (!format)
  152. return;
  153. va_start(ap, fmt);
  154. vsnprintf(format, maxlen, fmt, ap);
  155. va_end(ap);
  156. wpa_auth_logger(wpa_auth, addr, level, format);
  157. os_free(format);
  158. }
  159. static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
  160. const u8 *addr)
  161. {
  162. if (wpa_auth->cb.disconnect == NULL)
  163. return;
  164. wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
  165. WLAN_REASON_PREV_AUTH_NOT_VALID);
  166. }
  167. static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
  168. {
  169. int ret = 0;
  170. #ifdef CONFIG_IEEE80211R
  171. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  172. ret = 1;
  173. #endif /* CONFIG_IEEE80211R */
  174. #ifdef CONFIG_IEEE80211W
  175. if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
  176. ret = 1;
  177. #endif /* CONFIG_IEEE80211W */
  178. return ret;
  179. }
  180. static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
  181. {
  182. struct wpa_authenticator *wpa_auth = eloop_ctx;
  183. if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
  184. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  185. "initialization.");
  186. } else {
  187. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
  188. }
  189. if (wpa_auth->conf.wpa_gmk_rekey) {
  190. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  191. wpa_rekey_gmk, wpa_auth, NULL);
  192. }
  193. }
  194. static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
  195. {
  196. struct wpa_authenticator *wpa_auth = eloop_ctx;
  197. struct wpa_group *group;
  198. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
  199. for (group = wpa_auth->group; group; group = group->next) {
  200. group->GTKReKey = TRUE;
  201. do {
  202. group->changed = FALSE;
  203. wpa_group_sm_step(wpa_auth, group);
  204. } while (group->changed);
  205. }
  206. if (wpa_auth->conf.wpa_group_rekey) {
  207. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
  208. 0, wpa_rekey_gtk, wpa_auth, NULL);
  209. }
  210. }
  211. static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
  212. {
  213. struct wpa_authenticator *wpa_auth = eloop_ctx;
  214. struct wpa_state_machine *sm = timeout_ctx;
  215. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
  216. wpa_request_new_ptk(sm);
  217. wpa_sm_step(sm);
  218. }
  219. static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
  220. {
  221. if (sm->pmksa == ctx)
  222. sm->pmksa = NULL;
  223. return 0;
  224. }
  225. static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
  226. void *ctx)
  227. {
  228. struct wpa_authenticator *wpa_auth = ctx;
  229. wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
  230. }
  231. static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
  232. int vlan_id)
  233. {
  234. struct wpa_group *group;
  235. u8 buf[ETH_ALEN + 8 + sizeof(group)];
  236. u8 rkey[32];
  237. group = os_zalloc(sizeof(struct wpa_group));
  238. if (group == NULL)
  239. return NULL;
  240. group->GTKAuthenticator = TRUE;
  241. group->vlan_id = vlan_id;
  242. switch (wpa_auth->conf.wpa_group) {
  243. case WPA_CIPHER_CCMP:
  244. group->GTK_len = 16;
  245. break;
  246. case WPA_CIPHER_TKIP:
  247. group->GTK_len = 32;
  248. break;
  249. case WPA_CIPHER_WEP104:
  250. group->GTK_len = 13;
  251. break;
  252. case WPA_CIPHER_WEP40:
  253. group->GTK_len = 5;
  254. break;
  255. }
  256. /* Counter = PRF-256(Random number, "Init Counter",
  257. * Local MAC Address || Time)
  258. */
  259. os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
  260. wpa_get_ntp_timestamp(buf + ETH_ALEN);
  261. os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
  262. if (os_get_random(rkey, sizeof(rkey)) ||
  263. os_get_random(group->GMK, WPA_GMK_LEN)) {
  264. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  265. "initialization.");
  266. os_free(group);
  267. return NULL;
  268. }
  269. sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
  270. group->Counter, WPA_NONCE_LEN);
  271. group->GInit = TRUE;
  272. wpa_group_sm_step(wpa_auth, group);
  273. group->GInit = FALSE;
  274. wpa_group_sm_step(wpa_auth, group);
  275. return group;
  276. }
  277. /**
  278. * wpa_init - Initialize WPA authenticator
  279. * @addr: Authenticator address
  280. * @conf: Configuration for WPA authenticator
  281. * Returns: Pointer to WPA authenticator data or %NULL on failure
  282. */
  283. struct wpa_authenticator * wpa_init(const u8 *addr,
  284. struct wpa_auth_config *conf,
  285. struct wpa_auth_callbacks *cb)
  286. {
  287. struct wpa_authenticator *wpa_auth;
  288. wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
  289. if (wpa_auth == NULL)
  290. return NULL;
  291. os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
  292. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  293. os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
  294. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  295. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  296. os_free(wpa_auth);
  297. return NULL;
  298. }
  299. wpa_auth->group = wpa_group_init(wpa_auth, 0);
  300. if (wpa_auth->group == NULL) {
  301. os_free(wpa_auth->wpa_ie);
  302. os_free(wpa_auth);
  303. return NULL;
  304. }
  305. wpa_auth->pmksa = pmksa_cache_init(wpa_auth_pmksa_free_cb, wpa_auth);
  306. if (wpa_auth->pmksa == NULL) {
  307. wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
  308. os_free(wpa_auth->wpa_ie);
  309. os_free(wpa_auth);
  310. return NULL;
  311. }
  312. #ifdef CONFIG_IEEE80211R
  313. wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
  314. if (wpa_auth->ft_pmk_cache == NULL) {
  315. wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
  316. os_free(wpa_auth->wpa_ie);
  317. pmksa_cache_deinit(wpa_auth->pmksa);
  318. os_free(wpa_auth);
  319. return NULL;
  320. }
  321. #endif /* CONFIG_IEEE80211R */
  322. if (wpa_auth->conf.wpa_gmk_rekey) {
  323. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  324. wpa_rekey_gmk, wpa_auth, NULL);
  325. }
  326. if (wpa_auth->conf.wpa_group_rekey) {
  327. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
  328. wpa_rekey_gtk, wpa_auth, NULL);
  329. }
  330. return wpa_auth;
  331. }
  332. /**
  333. * wpa_deinit - Deinitialize WPA authenticator
  334. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  335. */
  336. void wpa_deinit(struct wpa_authenticator *wpa_auth)
  337. {
  338. struct wpa_group *group, *prev;
  339. eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
  340. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  341. #ifdef CONFIG_PEERKEY
  342. while (wpa_auth->stsl_negotiations)
  343. wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
  344. #endif /* CONFIG_PEERKEY */
  345. pmksa_cache_deinit(wpa_auth->pmksa);
  346. #ifdef CONFIG_IEEE80211R
  347. wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
  348. wpa_auth->ft_pmk_cache = NULL;
  349. #endif /* CONFIG_IEEE80211R */
  350. os_free(wpa_auth->wpa_ie);
  351. group = wpa_auth->group;
  352. while (group) {
  353. prev = group;
  354. group = group->next;
  355. os_free(prev);
  356. }
  357. os_free(wpa_auth);
  358. }
  359. /**
  360. * wpa_reconfig - Update WPA authenticator configuration
  361. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  362. * @conf: Configuration for WPA authenticator
  363. */
  364. int wpa_reconfig(struct wpa_authenticator *wpa_auth,
  365. struct wpa_auth_config *conf)
  366. {
  367. if (wpa_auth == NULL)
  368. return 0;
  369. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  370. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  371. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  372. return -1;
  373. }
  374. return 0;
  375. }
  376. struct wpa_state_machine *
  377. wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
  378. {
  379. struct wpa_state_machine *sm;
  380. sm = os_zalloc(sizeof(struct wpa_state_machine));
  381. if (sm == NULL)
  382. return NULL;
  383. os_memcpy(sm->addr, addr, ETH_ALEN);
  384. sm->wpa_auth = wpa_auth;
  385. sm->group = wpa_auth->group;
  386. return sm;
  387. }
  388. void wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
  389. struct wpa_state_machine *sm)
  390. {
  391. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  392. return;
  393. #ifdef CONFIG_IEEE80211R
  394. if (sm->ft_completed) {
  395. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  396. "FT authentication already completed - do not "
  397. "start 4-way handshake");
  398. return;
  399. }
  400. #endif /* CONFIG_IEEE80211R */
  401. if (sm->started) {
  402. os_memset(&sm->key_replay, 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. if (sm == NULL)
  934. return;
  935. __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
  936. keyidx, encr, 0);
  937. timeout_ms = pairwise ? dot11RSNAConfigPairwiseUpdateTimeOut :
  938. dot11RSNAConfigGroupUpdateTimeOut;
  939. eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
  940. wpa_send_eapol_timeout, wpa_auth, sm);
  941. }
  942. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
  943. {
  944. struct ieee802_1x_hdr *hdr;
  945. struct wpa_eapol_key *key;
  946. u16 key_info;
  947. int ret = 0;
  948. u8 mic[16];
  949. if (data_len < sizeof(*hdr) + sizeof(*key))
  950. return -1;
  951. hdr = (struct ieee802_1x_hdr *) data;
  952. key = (struct wpa_eapol_key *) (hdr + 1);
  953. key_info = WPA_GET_BE16(key->key_info);
  954. os_memcpy(mic, key->key_mic, 16);
  955. os_memset(key->key_mic, 0, 16);
  956. if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  957. data, data_len, key->key_mic) ||
  958. os_memcmp(mic, key->key_mic, 16) != 0)
  959. ret = -1;
  960. os_memcpy(key->key_mic, mic, 16);
  961. return ret;
  962. }
  963. void wpa_remove_ptk(struct wpa_state_machine *sm)
  964. {
  965. sm->PTK_valid = FALSE;
  966. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  967. wpa_auth_set_key(sm->wpa_auth, 0, "none", sm->addr, 0, (u8 *) "", 0);
  968. sm->pairwise_set = FALSE;
  969. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  970. }
  971. void wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
  972. {
  973. int remove_ptk = 1;
  974. if (sm == NULL)
  975. return;
  976. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  977. "event %d notification", event);
  978. switch (event) {
  979. case WPA_AUTH:
  980. case WPA_ASSOC:
  981. break;
  982. case WPA_DEAUTH:
  983. case WPA_DISASSOC:
  984. sm->DeauthenticationRequest = TRUE;
  985. break;
  986. case WPA_REAUTH:
  987. case WPA_REAUTH_EAPOL:
  988. if (sm->GUpdateStationKeys) {
  989. /*
  990. * Reauthentication cancels the pending group key
  991. * update for this STA.
  992. */
  993. sm->group->GKeyDoneStations--;
  994. sm->GUpdateStationKeys = FALSE;
  995. sm->PtkGroupInit = TRUE;
  996. }
  997. sm->ReAuthenticationRequest = TRUE;
  998. break;
  999. case WPA_ASSOC_FT:
  1000. #ifdef CONFIG_IEEE80211R
  1001. /* Using FT protocol, not WPA auth state machine */
  1002. sm->ft_completed = 1;
  1003. return;
  1004. #else /* CONFIG_IEEE80211R */
  1005. break;
  1006. #endif /* CONFIG_IEEE80211R */
  1007. }
  1008. #ifdef CONFIG_IEEE80211R
  1009. sm->ft_completed = 0;
  1010. #endif /* CONFIG_IEEE80211R */
  1011. #ifdef CONFIG_IEEE80211W
  1012. if (sm->mgmt_frame_prot && event == WPA_AUTH)
  1013. remove_ptk = 0;
  1014. #endif /* CONFIG_IEEE80211W */
  1015. if (remove_ptk) {
  1016. sm->PTK_valid = FALSE;
  1017. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1018. if (event != WPA_REAUTH_EAPOL)
  1019. wpa_remove_ptk(sm);
  1020. }
  1021. wpa_sm_step(sm);
  1022. }
  1023. static const char * wpa_alg_txt(int alg)
  1024. {
  1025. switch (alg) {
  1026. case WPA_CIPHER_CCMP:
  1027. return "CCMP";
  1028. case WPA_CIPHER_TKIP:
  1029. return "TKIP";
  1030. case WPA_CIPHER_WEP104:
  1031. case WPA_CIPHER_WEP40:
  1032. return "WEP";
  1033. default:
  1034. return "";
  1035. }
  1036. }
  1037. SM_STATE(WPA_PTK, INITIALIZE)
  1038. {
  1039. SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
  1040. if (sm->Init) {
  1041. /* Init flag is not cleared here, so avoid busy
  1042. * loop by claiming nothing changed. */
  1043. sm->changed = FALSE;
  1044. }
  1045. sm->keycount = 0;
  1046. if (sm->GUpdateStationKeys)
  1047. sm->group->GKeyDoneStations--;
  1048. sm->GUpdateStationKeys = FALSE;
  1049. if (sm->wpa == WPA_VERSION_WPA)
  1050. sm->PInitAKeys = FALSE;
  1051. if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
  1052. * Local AA > Remote AA)) */) {
  1053. sm->Pair = TRUE;
  1054. }
  1055. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
  1056. wpa_remove_ptk(sm);
  1057. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
  1058. sm->TimeoutCtr = 0;
  1059. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1060. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1061. WPA_EAPOL_authorized, 0);
  1062. }
  1063. }
  1064. SM_STATE(WPA_PTK, DISCONNECT)
  1065. {
  1066. SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
  1067. sm->Disconnect = FALSE;
  1068. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1069. }
  1070. SM_STATE(WPA_PTK, DISCONNECTED)
  1071. {
  1072. SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
  1073. sm->DeauthenticationRequest = FALSE;
  1074. }
  1075. SM_STATE(WPA_PTK, AUTHENTICATION)
  1076. {
  1077. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
  1078. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1079. sm->PTK_valid = FALSE;
  1080. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
  1081. 1);
  1082. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
  1083. sm->AuthenticationRequest = FALSE;
  1084. }
  1085. SM_STATE(WPA_PTK, AUTHENTICATION2)
  1086. {
  1087. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
  1088. os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
  1089. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  1090. sm->ReAuthenticationRequest = FALSE;
  1091. /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
  1092. * logical place than INITIALIZE since AUTHENTICATION2 can be
  1093. * re-entered on ReAuthenticationRequest without going through
  1094. * INITIALIZE. */
  1095. sm->TimeoutCtr = 0;
  1096. }
  1097. SM_STATE(WPA_PTK, INITPMK)
  1098. {
  1099. u8 msk[2 * PMK_LEN];
  1100. size_t len = 2 * PMK_LEN;
  1101. SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
  1102. #ifdef CONFIG_IEEE80211R
  1103. sm->xxkey_len = 0;
  1104. #endif /* CONFIG_IEEE80211R */
  1105. if (sm->pmksa) {
  1106. wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
  1107. os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
  1108. } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
  1109. wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
  1110. "(len=%lu)", (unsigned long) len);
  1111. os_memcpy(sm->PMK, msk, PMK_LEN);
  1112. #ifdef CONFIG_IEEE80211R
  1113. if (len >= 2 * PMK_LEN) {
  1114. os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
  1115. sm->xxkey_len = PMK_LEN;
  1116. }
  1117. #endif /* CONFIG_IEEE80211R */
  1118. } else {
  1119. wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
  1120. }
  1121. sm->req_replay_counter_used = 0;
  1122. /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
  1123. * will break reauthentication since EAPOL state machines may not be
  1124. * get into AUTHENTICATING state that clears keyRun before WPA state
  1125. * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
  1126. * state and takes PMK from the previously used AAA Key. This will
  1127. * eventually fail in 4-Way Handshake because Supplicant uses PMK
  1128. * derived from the new AAA Key. Setting keyRun = FALSE here seems to
  1129. * be good workaround for this issue. */
  1130. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
  1131. }
  1132. SM_STATE(WPA_PTK, INITPSK)
  1133. {
  1134. const u8 *psk;
  1135. SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
  1136. psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
  1137. if (psk) {
  1138. os_memcpy(sm->PMK, psk, PMK_LEN);
  1139. #ifdef CONFIG_IEEE80211R
  1140. os_memcpy(sm->xxkey, psk, PMK_LEN);
  1141. sm->xxkey_len = PMK_LEN;
  1142. #endif /* CONFIG_IEEE80211R */
  1143. }
  1144. sm->req_replay_counter_used = 0;
  1145. }
  1146. SM_STATE(WPA_PTK, PTKSTART)
  1147. {
  1148. u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
  1149. size_t pmkid_len = 0;
  1150. SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
  1151. sm->PTKRequest = FALSE;
  1152. sm->TimeoutEvt = FALSE;
  1153. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1154. "sending 1/4 msg of 4-Way Handshake");
  1155. /*
  1156. * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
  1157. * one possible PSK for this STA.
  1158. */
  1159. if (sm->wpa == WPA_VERSION_WPA2 &&
  1160. wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
  1161. pmkid = buf;
  1162. pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
  1163. pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
  1164. pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
  1165. RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
  1166. if (sm->pmksa)
  1167. os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
  1168. sm->pmksa->pmkid, PMKID_LEN);
  1169. else {
  1170. /*
  1171. * Calculate PMKID since no PMKSA cache entry was
  1172. * available with pre-calculated PMKID.
  1173. */
  1174. rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
  1175. sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
  1176. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1177. }
  1178. }
  1179. wpa_send_eapol(sm->wpa_auth, sm,
  1180. WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
  1181. sm->ANonce, pmkid, pmkid_len, 0, 0);
  1182. sm->TimeoutCtr++;
  1183. }
  1184. static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
  1185. struct wpa_ptk *ptk)
  1186. {
  1187. #ifdef CONFIG_IEEE80211R
  1188. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  1189. return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
  1190. #endif /* CONFIG_IEEE80211R */
  1191. wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
  1192. sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
  1193. (u8 *) ptk, sizeof(*ptk),
  1194. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1195. return 0;
  1196. }
  1197. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
  1198. {
  1199. struct wpa_ptk PTK;
  1200. int ok = 0;
  1201. const u8 *pmk = NULL;
  1202. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
  1203. sm->EAPOLKeyReceived = FALSE;
  1204. /* WPA with IEEE 802.1X: use the derived PMK from EAP
  1205. * WPA-PSK: iterate through possible PSKs and select the one matching
  1206. * the packet */
  1207. for (;;) {
  1208. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1209. pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
  1210. if (pmk == NULL)
  1211. break;
  1212. } else
  1213. pmk = sm->PMK;
  1214. wpa_derive_ptk(sm, pmk, &PTK);
  1215. if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
  1216. sm->last_rx_eapol_key_len) == 0) {
  1217. ok = 1;
  1218. break;
  1219. }
  1220. if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
  1221. break;
  1222. }
  1223. if (!ok) {
  1224. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1225. "invalid MIC in msg 2/4 of 4-Way Handshake");
  1226. return;
  1227. }
  1228. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  1229. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1230. /* PSK may have changed from the previous choice, so update
  1231. * state machine data based on whatever PSK was selected here.
  1232. */
  1233. os_memcpy(sm->PMK, pmk, PMK_LEN);
  1234. }
  1235. sm->MICVerified = TRUE;
  1236. os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
  1237. sm->PTK_valid = TRUE;
  1238. }
  1239. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
  1240. {
  1241. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
  1242. sm->TimeoutCtr = 0;
  1243. }
  1244. #ifdef CONFIG_IEEE80211W
  1245. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1246. {
  1247. if (sm->mgmt_frame_prot) {
  1248. return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
  1249. }
  1250. return 0;
  1251. }
  1252. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1253. {
  1254. struct wpa_igtk_kde igtk;
  1255. struct wpa_group *gsm = sm->group;
  1256. if (!sm->mgmt_frame_prot)
  1257. return pos;
  1258. igtk.keyid[0] = gsm->GN_igtk;
  1259. igtk.keyid[1] = 0;
  1260. if (wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn)
  1261. < 0)
  1262. os_memset(igtk.pn, 0, sizeof(igtk.pn));
  1263. os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
  1264. pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
  1265. (const u8 *) &igtk, sizeof(igtk), NULL, 0);
  1266. return pos;
  1267. }
  1268. #else /* CONFIG_IEEE80211W */
  1269. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1270. {
  1271. return 0;
  1272. }
  1273. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1274. {
  1275. return pos;
  1276. }
  1277. #endif /* CONFIG_IEEE80211W */
  1278. SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
  1279. {
  1280. u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
  1281. size_t gtk_len, kde_len;
  1282. struct wpa_group *gsm = sm->group;
  1283. u8 *wpa_ie;
  1284. int wpa_ie_len, secure, keyidx, encr = 0;
  1285. SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
  1286. sm->TimeoutEvt = FALSE;
  1287. /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
  1288. */
  1289. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1290. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1291. wpa_ie = sm->wpa_auth->wpa_ie;
  1292. wpa_ie_len = sm->wpa_auth->wpa_ie_len;
  1293. if (sm->wpa == WPA_VERSION_WPA &&
  1294. (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
  1295. wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
  1296. /* WPA-only STA, remove RSN IE */
  1297. wpa_ie = wpa_ie + wpa_ie[1] + 2;
  1298. wpa_ie_len = wpa_ie[1] + 2;
  1299. }
  1300. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1301. "sending 3/4 msg of 4-Way Handshake");
  1302. if (sm->wpa == WPA_VERSION_WPA2) {
  1303. /* WPA2 send GTK in the 4-way handshake */
  1304. secure = 1;
  1305. gtk = gsm->GTK[gsm->GN - 1];
  1306. gtk_len = gsm->GTK_len;
  1307. keyidx = gsm->GN;
  1308. _rsc = rsc;
  1309. encr = 1;
  1310. } else {
  1311. /* WPA does not include GTK in msg 3/4 */
  1312. secure = 0;
  1313. gtk = NULL;
  1314. gtk_len = 0;
  1315. keyidx = 0;
  1316. _rsc = NULL;
  1317. }
  1318. kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
  1319. if (gtk)
  1320. kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
  1321. kde = os_malloc(kde_len);
  1322. if (kde == NULL)
  1323. return;
  1324. pos = kde;
  1325. os_memcpy(pos, wpa_ie, wpa_ie_len);
  1326. pos += wpa_ie_len;
  1327. if (gtk) {
  1328. u8 hdr[2];
  1329. hdr[0] = keyidx & 0x03;
  1330. hdr[1] = 0;
  1331. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1332. gtk, gtk_len);
  1333. }
  1334. pos = ieee80211w_kde_add(sm, pos);
  1335. wpa_send_eapol(sm->wpa_auth, sm,
  1336. (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
  1337. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
  1338. WPA_KEY_INFO_KEY_TYPE,
  1339. _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
  1340. os_free(kde);
  1341. sm->TimeoutCtr++;
  1342. }
  1343. SM_STATE(WPA_PTK, PTKINITDONE)
  1344. {
  1345. SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
  1346. sm->EAPOLKeyReceived = FALSE;
  1347. if (sm->Pair) {
  1348. char *alg;
  1349. int klen;
  1350. if (sm->pairwise == WPA_CIPHER_TKIP) {
  1351. alg = "TKIP";
  1352. klen = 32;
  1353. } else {
  1354. alg = "CCMP";
  1355. klen = 16;
  1356. }
  1357. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  1358. sm->PTK.tk1, klen)) {
  1359. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1360. return;
  1361. }
  1362. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  1363. sm->pairwise_set = TRUE;
  1364. if (sm->wpa_auth->conf.wpa_ptk_rekey) {
  1365. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  1366. eloop_register_timeout(sm->wpa_auth->conf.
  1367. wpa_ptk_rekey, 0, wpa_rekey_ptk,
  1368. sm->wpa_auth, sm);
  1369. }
  1370. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1371. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1372. WPA_EAPOL_authorized, 1);
  1373. }
  1374. }
  1375. if (0 /* IBSS == TRUE */) {
  1376. sm->keycount++;
  1377. if (sm->keycount == 2) {
  1378. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1379. WPA_EAPOL_portValid, 1);
  1380. }
  1381. } else {
  1382. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
  1383. 1);
  1384. }
  1385. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
  1386. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
  1387. if (sm->wpa == WPA_VERSION_WPA)
  1388. sm->PInitAKeys = TRUE;
  1389. else
  1390. sm->has_GTK = TRUE;
  1391. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1392. "pairwise key handshake completed (%s)",
  1393. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1394. #ifdef CONFIG_IEEE80211R
  1395. wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
  1396. #endif /* CONFIG_IEEE80211R */
  1397. }
  1398. SM_STEP(WPA_PTK)
  1399. {
  1400. struct wpa_authenticator *wpa_auth = sm->wpa_auth;
  1401. if (sm->Init)
  1402. SM_ENTER(WPA_PTK, INITIALIZE);
  1403. else if (sm->Disconnect
  1404. /* || FIX: dot11RSNAConfigSALifetime timeout */)
  1405. SM_ENTER(WPA_PTK, DISCONNECT);
  1406. else if (sm->DeauthenticationRequest)
  1407. SM_ENTER(WPA_PTK, DISCONNECTED);
  1408. else if (sm->AuthenticationRequest)
  1409. SM_ENTER(WPA_PTK, AUTHENTICATION);
  1410. else if (sm->ReAuthenticationRequest)
  1411. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1412. else if (sm->PTKRequest)
  1413. SM_ENTER(WPA_PTK, PTKSTART);
  1414. else switch (sm->wpa_ptk_state) {
  1415. case WPA_PTK_INITIALIZE:
  1416. break;
  1417. case WPA_PTK_DISCONNECT:
  1418. SM_ENTER(WPA_PTK, DISCONNECTED);
  1419. break;
  1420. case WPA_PTK_DISCONNECTED:
  1421. SM_ENTER(WPA_PTK, INITIALIZE);
  1422. break;
  1423. case WPA_PTK_AUTHENTICATION:
  1424. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1425. break;
  1426. case WPA_PTK_AUTHENTICATION2:
  1427. if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  1428. wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1429. WPA_EAPOL_keyRun) > 0)
  1430. SM_ENTER(WPA_PTK, INITPMK);
  1431. else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
  1432. /* FIX: && 802.1X::keyRun */)
  1433. SM_ENTER(WPA_PTK, INITPSK);
  1434. break;
  1435. case WPA_PTK_INITPMK:
  1436. if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1437. WPA_EAPOL_keyAvailable) > 0)
  1438. SM_ENTER(WPA_PTK, PTKSTART);
  1439. else {
  1440. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1441. SM_ENTER(WPA_PTK, DISCONNECT);
  1442. }
  1443. break;
  1444. case WPA_PTK_INITPSK:
  1445. if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
  1446. SM_ENTER(WPA_PTK, PTKSTART);
  1447. else {
  1448. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1449. "no PSK configured for the STA");
  1450. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1451. SM_ENTER(WPA_PTK, DISCONNECT);
  1452. }
  1453. break;
  1454. case WPA_PTK_PTKSTART:
  1455. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1456. sm->EAPOLKeyPairwise)
  1457. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1458. else if (sm->TimeoutCtr >
  1459. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1460. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1461. SM_ENTER(WPA_PTK, DISCONNECT);
  1462. } else if (sm->TimeoutEvt)
  1463. SM_ENTER(WPA_PTK, PTKSTART);
  1464. break;
  1465. case WPA_PTK_PTKCALCNEGOTIATING:
  1466. if (sm->MICVerified)
  1467. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
  1468. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1469. sm->EAPOLKeyPairwise)
  1470. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1471. else if (sm->TimeoutEvt)
  1472. SM_ENTER(WPA_PTK, PTKSTART);
  1473. break;
  1474. case WPA_PTK_PTKCALCNEGOTIATING2:
  1475. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1476. break;
  1477. case WPA_PTK_PTKINITNEGOTIATING:
  1478. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1479. sm->EAPOLKeyPairwise && sm->MICVerified)
  1480. SM_ENTER(WPA_PTK, PTKINITDONE);
  1481. else if (sm->TimeoutCtr >
  1482. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1483. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1484. SM_ENTER(WPA_PTK, DISCONNECT);
  1485. } else if (sm->TimeoutEvt)
  1486. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1487. break;
  1488. case WPA_PTK_PTKINITDONE:
  1489. break;
  1490. }
  1491. }
  1492. SM_STATE(WPA_PTK_GROUP, IDLE)
  1493. {
  1494. SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
  1495. if (sm->Init) {
  1496. /* Init flag is not cleared here, so avoid busy
  1497. * loop by claiming nothing changed. */
  1498. sm->changed = FALSE;
  1499. }
  1500. sm->GTimeoutCtr = 0;
  1501. }
  1502. SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
  1503. {
  1504. u8 rsc[WPA_KEY_RSC_LEN];
  1505. struct wpa_group *gsm = sm->group;
  1506. u8 *kde, *pos, hdr[2];
  1507. size_t kde_len;
  1508. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
  1509. if (sm->wpa == WPA_VERSION_WPA)
  1510. sm->PInitAKeys = FALSE;
  1511. sm->TimeoutEvt = FALSE;
  1512. /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
  1513. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1514. if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
  1515. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1516. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1517. "sending 1/2 msg of Group Key Handshake");
  1518. if (sm->wpa == WPA_VERSION_WPA2) {
  1519. kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
  1520. ieee80211w_kde_len(sm);
  1521. kde = os_malloc(kde_len);
  1522. if (kde == NULL)
  1523. return;
  1524. pos = kde;
  1525. hdr[0] = gsm->GN & 0x03;
  1526. hdr[1] = 0;
  1527. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1528. gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  1529. pos = ieee80211w_kde_add(sm, pos);
  1530. } else {
  1531. kde = gsm->GTK[gsm->GN - 1];
  1532. pos = kde + gsm->GTK_len;
  1533. }
  1534. wpa_send_eapol(sm->wpa_auth, sm,
  1535. WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  1536. WPA_KEY_INFO_ACK |
  1537. (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
  1538. rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
  1539. if (sm->wpa == WPA_VERSION_WPA2)
  1540. os_free(kde);
  1541. sm->GTimeoutCtr++;
  1542. }
  1543. SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
  1544. {
  1545. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
  1546. sm->EAPOLKeyReceived = FALSE;
  1547. if (sm->GUpdateStationKeys)
  1548. sm->group->GKeyDoneStations--;
  1549. sm->GUpdateStationKeys = FALSE;
  1550. sm->GTimeoutCtr = 0;
  1551. /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
  1552. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1553. "group key handshake completed (%s)",
  1554. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1555. sm->has_GTK = TRUE;
  1556. }
  1557. SM_STATE(WPA_PTK_GROUP, KEYERROR)
  1558. {
  1559. SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
  1560. if (sm->GUpdateStationKeys)
  1561. sm->group->GKeyDoneStations--;
  1562. sm->GUpdateStationKeys = FALSE;
  1563. sm->Disconnect = TRUE;
  1564. }
  1565. SM_STEP(WPA_PTK_GROUP)
  1566. {
  1567. if (sm->Init || sm->PtkGroupInit) {
  1568. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1569. sm->PtkGroupInit = FALSE;
  1570. } else switch (sm->wpa_ptk_group_state) {
  1571. case WPA_PTK_GROUP_IDLE:
  1572. if (sm->GUpdateStationKeys ||
  1573. (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
  1574. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  1575. break;
  1576. case WPA_PTK_GROUP_REKEYNEGOTIATING:
  1577. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1578. !sm->EAPOLKeyPairwise && sm->MICVerified)
  1579. SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
  1580. else if (sm->GTimeoutCtr >
  1581. (int) dot11RSNAConfigGroupUpdateCount)
  1582. SM_ENTER(WPA_PTK_GROUP, KEYERROR);
  1583. else if (sm->TimeoutEvt)
  1584. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  1585. break;
  1586. case WPA_PTK_GROUP_KEYERROR:
  1587. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1588. break;
  1589. case WPA_PTK_GROUP_REKEYESTABLISHED:
  1590. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1591. break;
  1592. }
  1593. }
  1594. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  1595. struct wpa_group *group)
  1596. {
  1597. int ret = 0;
  1598. /* FIX: is this the correct way of getting GNonce? */
  1599. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  1600. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  1601. wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
  1602. group->GTK[group->GN - 1], group->GTK_len);
  1603. #ifdef CONFIG_IEEE80211W
  1604. if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
  1605. if (os_get_random(group->IGTK[group->GN_igtk - 4],
  1606. WPA_IGTK_LEN) < 0) {
  1607. wpa_printf(MSG_INFO, "RSN: Failed to get new random "
  1608. "IGTK");
  1609. ret = -1;
  1610. }
  1611. wpa_hexdump_key(MSG_DEBUG, "IGTK",
  1612. group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
  1613. }
  1614. #endif /* CONFIG_IEEE80211W */
  1615. return ret;
  1616. }
  1617. static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
  1618. struct wpa_group *group)
  1619. {
  1620. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1621. "GTK_INIT (VLAN-ID %d)", group->vlan_id);
  1622. group->changed = FALSE; /* GInit is not cleared here; avoid loop */
  1623. group->wpa_group_state = WPA_GROUP_GTK_INIT;
  1624. /* GTK[0..N] = 0 */
  1625. os_memset(group->GTK, 0, sizeof(group->GTK));
  1626. group->GN = 1;
  1627. group->GM = 2;
  1628. #ifdef CONFIG_IEEE80211W
  1629. group->GN_igtk = 4;
  1630. group->GM_igtk = 5;
  1631. #endif /* CONFIG_IEEE80211W */
  1632. /* GTK[GN] = CalcGTK() */
  1633. wpa_gtk_update(wpa_auth, group);
  1634. }
  1635. static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
  1636. {
  1637. if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
  1638. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1639. "Not in PTKINITDONE; skip Group Key update");
  1640. return 0;
  1641. }
  1642. if (sm->GUpdateStationKeys) {
  1643. /*
  1644. * This should not really happen, but just in case, make sure
  1645. * we do not count the same STA twice in GKeyDoneStations.
  1646. */
  1647. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1648. "GUpdateStationKeys already set - do not "
  1649. "increment GKeyDoneStations");
  1650. } else {
  1651. sm->group->GKeyDoneStations++;
  1652. sm->GUpdateStationKeys = TRUE;
  1653. }
  1654. wpa_sm_step(sm);
  1655. return 0;
  1656. }
  1657. static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
  1658. struct wpa_group *group)
  1659. {
  1660. int tmp;
  1661. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1662. "SETKEYS (VLAN-ID %d)", group->vlan_id);
  1663. group->changed = TRUE;
  1664. group->wpa_group_state = WPA_GROUP_SETKEYS;
  1665. group->GTKReKey = FALSE;
  1666. tmp = group->GM;
  1667. group->GM = group->GN;
  1668. group->GN = tmp;
  1669. #ifdef CONFIG_IEEE80211W
  1670. tmp = group->GM_igtk;
  1671. group->GM_igtk = group->GN_igtk;
  1672. group->GN_igtk = tmp;
  1673. #endif /* CONFIG_IEEE80211W */
  1674. /* "GKeyDoneStations = GNoStations" is done in more robust way by
  1675. * counting the STAs that are marked with GUpdateStationKeys instead of
  1676. * including all STAs that could be in not-yet-completed state. */
  1677. wpa_gtk_update(wpa_auth, group);
  1678. wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
  1679. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
  1680. group->GKeyDoneStations);
  1681. }
  1682. static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
  1683. struct wpa_group *group)
  1684. {
  1685. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1686. "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
  1687. group->changed = TRUE;
  1688. group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
  1689. wpa_auth_set_key(wpa_auth, group->vlan_id,
  1690. wpa_alg_txt(wpa_auth->conf.wpa_group),
  1691. NULL, group->GN, group->GTK[group->GN - 1],
  1692. group->GTK_len);
  1693. #ifdef CONFIG_IEEE80211W
  1694. if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
  1695. wpa_auth_set_key(wpa_auth, group->vlan_id, "IGTK",
  1696. NULL, group->GN_igtk,
  1697. group->IGTK[group->GN_igtk - 4],
  1698. WPA_IGTK_LEN);
  1699. }
  1700. #endif /* CONFIG_IEEE80211W */
  1701. }
  1702. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  1703. struct wpa_group *group)
  1704. {
  1705. if (group->GInit) {
  1706. wpa_group_gtk_init(wpa_auth, group);
  1707. } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
  1708. group->GTKAuthenticator) {
  1709. wpa_group_setkeysdone(wpa_auth, group);
  1710. } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
  1711. group->GTKReKey) {
  1712. wpa_group_setkeys(wpa_auth, group);
  1713. } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
  1714. if (group->GKeyDoneStations == 0)
  1715. wpa_group_setkeysdone(wpa_auth, group);
  1716. else if (group->GTKReKey)
  1717. wpa_group_setkeys(wpa_auth, group);
  1718. }
  1719. }
  1720. static void wpa_sm_step(struct wpa_state_machine *sm)
  1721. {
  1722. if (sm == NULL)
  1723. return;
  1724. if (sm->in_step_loop) {
  1725. /* This should not happen, but if it does, make sure we do not
  1726. * end up freeing the state machine too early by exiting the
  1727. * recursive call. */
  1728. wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
  1729. return;
  1730. }
  1731. sm->in_step_loop = 1;
  1732. do {
  1733. if (sm->pending_deinit)
  1734. break;
  1735. sm->changed = FALSE;
  1736. sm->wpa_auth->group->changed = FALSE;
  1737. SM_STEP_RUN(WPA_PTK);
  1738. if (sm->pending_deinit)
  1739. break;
  1740. SM_STEP_RUN(WPA_PTK_GROUP);
  1741. if (sm->pending_deinit)
  1742. break;
  1743. wpa_group_sm_step(sm->wpa_auth, sm->group);
  1744. } while (sm->changed || sm->wpa_auth->group->changed);
  1745. sm->in_step_loop = 0;
  1746. if (sm->pending_deinit) {
  1747. wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
  1748. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  1749. wpa_free_sta_sm(sm);
  1750. }
  1751. }
  1752. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
  1753. {
  1754. struct wpa_state_machine *sm = eloop_ctx;
  1755. wpa_sm_step(sm);
  1756. }
  1757. void wpa_auth_sm_notify(struct wpa_state_machine *sm)
  1758. {
  1759. if (sm == NULL)
  1760. return;
  1761. eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
  1762. }
  1763. void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
  1764. {
  1765. int tmp, i;
  1766. struct wpa_group *group;
  1767. if (wpa_auth == NULL)
  1768. return;
  1769. group = wpa_auth->group;
  1770. for (i = 0; i < 2; i++) {
  1771. tmp = group->GM;
  1772. group->GM = group->GN;
  1773. group->GN = tmp;
  1774. #ifdef CONFIG_IEEE80211W
  1775. tmp = group->GM_igtk;
  1776. group->GM_igtk = group->GN_igtk;
  1777. group->GN_igtk = tmp;
  1778. #endif /* CONFIG_IEEE80211W */
  1779. wpa_gtk_update(wpa_auth, group);
  1780. }
  1781. }
  1782. static const char * wpa_bool_txt(int bool)
  1783. {
  1784. return bool ? "TRUE" : "FALSE";
  1785. }
  1786. static int wpa_cipher_bits(int cipher)
  1787. {
  1788. switch (cipher) {
  1789. case WPA_CIPHER_CCMP:
  1790. return 128;
  1791. case WPA_CIPHER_TKIP:
  1792. return 256;
  1793. case WPA_CIPHER_WEP104:
  1794. return 104;
  1795. case WPA_CIPHER_WEP40:
  1796. return 40;
  1797. default:
  1798. return 0;
  1799. }
  1800. }
  1801. #define RSN_SUITE "%02x-%02x-%02x-%d"
  1802. #define RSN_SUITE_ARG(s) \
  1803. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  1804. int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
  1805. {
  1806. int len = 0, ret;
  1807. char pmkid_txt[PMKID_LEN * 2 + 1];
  1808. if (wpa_auth == NULL)
  1809. return len;
  1810. ret = os_snprintf(buf + len, buflen - len,
  1811. "dot11RSNAOptionImplemented=TRUE\n"
  1812. #ifdef CONFIG_RSN_PREAUTH
  1813. "dot11RSNAPreauthenticationImplemented=TRUE\n"
  1814. #else /* CONFIG_RSN_PREAUTH */
  1815. "dot11RSNAPreauthenticationImplemented=FALSE\n"
  1816. #endif /* CONFIG_RSN_PREAUTH */
  1817. "dot11RSNAEnabled=%s\n"
  1818. "dot11RSNAPreauthenticationEnabled=%s\n",
  1819. wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
  1820. wpa_bool_txt(wpa_auth->conf.rsn_preauth));
  1821. if (ret < 0 || (size_t) ret >= buflen - len)
  1822. return len;
  1823. len += ret;
  1824. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  1825. wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
  1826. ret = os_snprintf(
  1827. buf + len, buflen - len,
  1828. "dot11RSNAConfigVersion=%u\n"
  1829. "dot11RSNAConfigPairwiseKeysSupported=9999\n"
  1830. /* FIX: dot11RSNAConfigGroupCipher */
  1831. /* FIX: dot11RSNAConfigGroupRekeyMethod */
  1832. /* FIX: dot11RSNAConfigGroupRekeyTime */
  1833. /* FIX: dot11RSNAConfigGroupRekeyPackets */
  1834. "dot11RSNAConfigGroupRekeyStrict=%u\n"
  1835. "dot11RSNAConfigGroupUpdateCount=%u\n"
  1836. "dot11RSNAConfigPairwiseUpdateCount=%u\n"
  1837. "dot11RSNAConfigGroupCipherSize=%u\n"
  1838. "dot11RSNAConfigPMKLifetime=%u\n"
  1839. "dot11RSNAConfigPMKReauthThreshold=%u\n"
  1840. "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
  1841. "dot11RSNAConfigSATimeout=%u\n"
  1842. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  1843. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  1844. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  1845. "dot11RSNAPMKIDUsed=%s\n"
  1846. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  1847. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  1848. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  1849. "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
  1850. "dot11RSNA4WayHandshakeFailures=%u\n"
  1851. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
  1852. RSN_VERSION,
  1853. !!wpa_auth->conf.wpa_strict_rekey,
  1854. dot11RSNAConfigGroupUpdateCount,
  1855. dot11RSNAConfigPairwiseUpdateCount,
  1856. wpa_cipher_bits(wpa_auth->conf.wpa_group),
  1857. dot11RSNAConfigPMKLifetime,
  1858. dot11RSNAConfigPMKReauthThreshold,
  1859. dot11RSNAConfigSATimeout,
  1860. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
  1861. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
  1862. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
  1863. pmkid_txt,
  1864. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
  1865. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
  1866. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
  1867. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
  1868. wpa_auth->dot11RSNA4WayHandshakeFailures);
  1869. if (ret < 0 || (size_t) ret >= buflen - len)
  1870. return len;
  1871. len += ret;
  1872. /* TODO: dot11RSNAConfigPairwiseCiphersTable */
  1873. /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
  1874. /* Private MIB */
  1875. ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
  1876. wpa_auth->group->wpa_group_state);
  1877. if (ret < 0 || (size_t) ret >= buflen - len)
  1878. return len;
  1879. len += ret;
  1880. return len;
  1881. }
  1882. int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
  1883. {
  1884. int len = 0, ret;
  1885. u32 pairwise = 0;
  1886. if (sm == NULL)
  1887. return 0;
  1888. /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
  1889. /* dot11RSNAStatsEntry */
  1890. if (sm->wpa == WPA_VERSION_WPA) {
  1891. if (sm->pairwise == WPA_CIPHER_CCMP)
  1892. pairwise = WPA_CIPHER_SUITE_CCMP;
  1893. else if (sm->pairwise == WPA_CIPHER_TKIP)
  1894. pairwise = WPA_CIPHER_SUITE_TKIP;
  1895. else if (sm->pairwise == WPA_CIPHER_WEP104)
  1896. pairwise = WPA_CIPHER_SUITE_WEP104;
  1897. else if (sm->pairwise == WPA_CIPHER_WEP40)
  1898. pairwise = WPA_CIPHER_SUITE_WEP40;
  1899. else if (sm->pairwise == WPA_CIPHER_NONE)
  1900. pairwise = WPA_CIPHER_SUITE_NONE;
  1901. } else if (sm->wpa == WPA_VERSION_WPA2) {
  1902. if (sm->pairwise == WPA_CIPHER_CCMP)
  1903. pairwise = RSN_CIPHER_SUITE_CCMP;
  1904. else if (sm->pairwise == WPA_CIPHER_TKIP)
  1905. pairwise = RSN_CIPHER_SUITE_TKIP;
  1906. else if (sm->pairwise == WPA_CIPHER_WEP104)
  1907. pairwise = RSN_CIPHER_SUITE_WEP104;
  1908. else if (sm->pairwise == WPA_CIPHER_WEP40)
  1909. pairwise = RSN_CIPHER_SUITE_WEP40;
  1910. else if (sm->pairwise == WPA_CIPHER_NONE)
  1911. pairwise = RSN_CIPHER_SUITE_NONE;
  1912. } else
  1913. return 0;
  1914. ret = os_snprintf(
  1915. buf + len, buflen - len,
  1916. /* TODO: dot11RSNAStatsIndex */
  1917. "dot11RSNAStatsSTAAddress=" MACSTR "\n"
  1918. "dot11RSNAStatsVersion=1\n"
  1919. "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
  1920. /* TODO: dot11RSNAStatsTKIPICVErrors */
  1921. "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
  1922. "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
  1923. /* TODO: dot11RSNAStatsCCMPReplays */
  1924. /* TODO: dot11RSNAStatsCCMPDecryptErrors */
  1925. /* TODO: dot11RSNAStatsTKIPReplays */,
  1926. MAC2STR(sm->addr),
  1927. RSN_SUITE_ARG(pairwise),
  1928. sm->dot11RSNAStatsTKIPLocalMICFailures,
  1929. sm->dot11RSNAStatsTKIPRemoteMICFailures);
  1930. if (ret < 0 || (size_t) ret >= buflen - len)
  1931. return len;
  1932. len += ret;
  1933. /* Private MIB */
  1934. ret = os_snprintf(buf + len, buflen - len,
  1935. "hostapdWPAPTKState=%d\n"
  1936. "hostapdWPAPTKGroupState=%d\n",
  1937. sm->wpa_ptk_state,
  1938. sm->wpa_ptk_group_state);
  1939. if (ret < 0 || (size_t) ret >= buflen - len)
  1940. return len;
  1941. len += ret;
  1942. return len;
  1943. }
  1944. void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
  1945. {
  1946. if (wpa_auth)
  1947. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
  1948. }
  1949. int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
  1950. {
  1951. return sm && sm->pairwise_set;
  1952. }
  1953. int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
  1954. {
  1955. if (sm == NULL)
  1956. return -1;
  1957. return sm->wpa_key_mgmt;
  1958. }
  1959. int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
  1960. {
  1961. if (sm == NULL)
  1962. return 0;
  1963. return sm->wpa;
  1964. }
  1965. int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
  1966. struct rsn_pmksa_cache_entry *entry)
  1967. {
  1968. if (sm == NULL || sm->pmksa != entry)
  1969. return -1;
  1970. sm->pmksa = NULL;
  1971. return 0;
  1972. }
  1973. struct rsn_pmksa_cache_entry *
  1974. wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
  1975. {
  1976. return sm ? sm->pmksa : NULL;
  1977. }
  1978. void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
  1979. {
  1980. if (sm)
  1981. sm->dot11RSNAStatsTKIPLocalMICFailures++;
  1982. }
  1983. const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
  1984. {
  1985. if (wpa_auth == NULL)
  1986. return NULL;
  1987. *len = wpa_auth->wpa_ie_len;
  1988. return wpa_auth->wpa_ie;
  1989. }
  1990. int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
  1991. int session_timeout, struct eapol_state_machine *eapol)
  1992. {
  1993. if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
  1994. return -1;
  1995. if (pmksa_cache_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
  1996. sm->wpa_auth->addr, sm->addr, session_timeout,
  1997. eapol, sm->wpa_key_mgmt))
  1998. return 0;
  1999. return -1;
  2000. }
  2001. int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
  2002. const u8 *pmk, size_t len, const u8 *sta_addr,
  2003. int session_timeout,
  2004. struct eapol_state_machine *eapol)
  2005. {
  2006. if (wpa_auth == NULL)
  2007. return -1;
  2008. if (pmksa_cache_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
  2009. sta_addr, session_timeout, eapol,
  2010. WPA_KEY_MGMT_IEEE8021X))
  2011. return 0;
  2012. return -1;
  2013. }
  2014. static struct wpa_group *
  2015. wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  2016. {
  2017. struct wpa_group *group;
  2018. if (wpa_auth == NULL || wpa_auth->group == NULL)
  2019. return NULL;
  2020. wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
  2021. vlan_id);
  2022. group = wpa_group_init(wpa_auth, vlan_id);
  2023. if (group == NULL)
  2024. return NULL;
  2025. group->next = wpa_auth->group->next;
  2026. wpa_auth->group->next = group;
  2027. return group;
  2028. }
  2029. int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
  2030. {
  2031. struct wpa_group *group;
  2032. if (sm == NULL || sm->wpa_auth == NULL)
  2033. return 0;
  2034. group = sm->wpa_auth->group;
  2035. while (group) {
  2036. if (group->vlan_id == vlan_id)
  2037. break;
  2038. group = group->next;
  2039. }
  2040. if (group == NULL) {
  2041. group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
  2042. if (group == NULL)
  2043. return -1;
  2044. }
  2045. if (sm->group == group)
  2046. return 0;
  2047. wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
  2048. "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
  2049. sm->group = group;
  2050. return 0;
  2051. }
  2052. #endif /* CONFIG_NATIVE_WINDOWS */