wpa_auth.c 82 KB

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