wpa_auth.c 90 KB

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