wpa_auth.c 73 KB

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