wpa_auth.c 90 KB

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