wpa_auth.c 97 KB

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