wpa_auth.c 76 KB

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