wpa_auth.c 77 KB

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