eap.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145
  1. /*
  2. * EAP peer state machines (RFC 4137)
  3. * Copyright (c) 2004-2014, 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. * This file implements the Peer State Machine as defined in RFC 4137. The used
  9. * states and state transitions match mostly with the RFC. However, there are
  10. * couple of additional transitions for working around small issues noticed
  11. * during testing. These exceptions are explained in comments within the
  12. * functions in this file. The method functions, m.func(), are similar to the
  13. * ones used in RFC 4137, but some small changes have used here to optimize
  14. * operations and to add functionality needed for fast re-authentication
  15. * (session resumption).
  16. */
  17. #include "includes.h"
  18. #include "common.h"
  19. #include "pcsc_funcs.h"
  20. #include "state_machine.h"
  21. #include "ext_password.h"
  22. #include "crypto/crypto.h"
  23. #include "crypto/tls.h"
  24. #include "crypto/sha256.h"
  25. #include "common/wpa_ctrl.h"
  26. #include "eap_common/eap_wsc_common.h"
  27. #include "eap_i.h"
  28. #include "eap_config.h"
  29. #define STATE_MACHINE_DATA struct eap_sm
  30. #define STATE_MACHINE_DEBUG_PREFIX "EAP"
  31. #define EAP_MAX_AUTH_ROUNDS 50
  32. #define EAP_CLIENT_TIMEOUT_DEFAULT 60
  33. static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
  34. EapType method);
  35. static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id);
  36. static void eap_sm_processIdentity(struct eap_sm *sm,
  37. const struct wpabuf *req);
  38. static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req);
  39. static struct wpabuf * eap_sm_buildNotify(int id);
  40. static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req);
  41. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  42. static const char * eap_sm_method_state_txt(EapMethodState state);
  43. static const char * eap_sm_decision_txt(EapDecision decision);
  44. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  45. static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field,
  46. const char *msg, size_t msglen);
  47. static Boolean eapol_get_bool(struct eap_sm *sm, enum eapol_bool_var var)
  48. {
  49. return sm->eapol_cb->get_bool(sm->eapol_ctx, var);
  50. }
  51. static void eapol_set_bool(struct eap_sm *sm, enum eapol_bool_var var,
  52. Boolean value)
  53. {
  54. sm->eapol_cb->set_bool(sm->eapol_ctx, var, value);
  55. }
  56. static unsigned int eapol_get_int(struct eap_sm *sm, enum eapol_int_var var)
  57. {
  58. return sm->eapol_cb->get_int(sm->eapol_ctx, var);
  59. }
  60. static void eapol_set_int(struct eap_sm *sm, enum eapol_int_var var,
  61. unsigned int value)
  62. {
  63. sm->eapol_cb->set_int(sm->eapol_ctx, var, value);
  64. }
  65. static struct wpabuf * eapol_get_eapReqData(struct eap_sm *sm)
  66. {
  67. return sm->eapol_cb->get_eapReqData(sm->eapol_ctx);
  68. }
  69. static void eap_notify_status(struct eap_sm *sm, const char *status,
  70. const char *parameter)
  71. {
  72. wpa_printf(MSG_DEBUG, "EAP: Status notification: %s (param=%s)",
  73. status, parameter);
  74. if (sm->eapol_cb->notify_status)
  75. sm->eapol_cb->notify_status(sm->eapol_ctx, status, parameter);
  76. }
  77. static void eap_sm_free_key(struct eap_sm *sm)
  78. {
  79. if (sm->eapKeyData) {
  80. bin_clear_free(sm->eapKeyData, sm->eapKeyDataLen);
  81. sm->eapKeyData = NULL;
  82. }
  83. }
  84. static void eap_deinit_prev_method(struct eap_sm *sm, const char *txt)
  85. {
  86. ext_password_free(sm->ext_pw_buf);
  87. sm->ext_pw_buf = NULL;
  88. if (sm->m == NULL || sm->eap_method_priv == NULL)
  89. return;
  90. wpa_printf(MSG_DEBUG, "EAP: deinitialize previously used EAP method "
  91. "(%d, %s) at %s", sm->selectedMethod, sm->m->name, txt);
  92. sm->m->deinit(sm, sm->eap_method_priv);
  93. sm->eap_method_priv = NULL;
  94. sm->m = NULL;
  95. }
  96. /**
  97. * eap_config_allowed_method - Check whether EAP method is allowed
  98. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  99. * @config: EAP configuration
  100. * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
  101. * @method: EAP type
  102. * Returns: 1 = allowed EAP method, 0 = not allowed
  103. */
  104. static int eap_config_allowed_method(struct eap_sm *sm,
  105. struct eap_peer_config *config,
  106. int vendor, u32 method)
  107. {
  108. int i;
  109. struct eap_method_type *m;
  110. if (config == NULL || config->eap_methods == NULL)
  111. return 1;
  112. m = config->eap_methods;
  113. for (i = 0; m[i].vendor != EAP_VENDOR_IETF ||
  114. m[i].method != EAP_TYPE_NONE; i++) {
  115. if (m[i].vendor == vendor && m[i].method == method)
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. /**
  121. * eap_allowed_method - Check whether EAP method is allowed
  122. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  123. * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
  124. * @method: EAP type
  125. * Returns: 1 = allowed EAP method, 0 = not allowed
  126. */
  127. int eap_allowed_method(struct eap_sm *sm, int vendor, u32 method)
  128. {
  129. return eap_config_allowed_method(sm, eap_get_config(sm), vendor,
  130. method);
  131. }
  132. #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
  133. static int eap_sm_append_3gpp_realm(struct eap_sm *sm, char *imsi,
  134. size_t max_len, size_t *imsi_len,
  135. int mnc_len)
  136. {
  137. char *pos, mnc[4];
  138. if (*imsi_len + 36 > max_len) {
  139. wpa_printf(MSG_WARNING, "No room for realm in IMSI buffer");
  140. return -1;
  141. }
  142. if (mnc_len != 2 && mnc_len != 3)
  143. mnc_len = 3;
  144. if (mnc_len == 2) {
  145. mnc[0] = '0';
  146. mnc[1] = imsi[3];
  147. mnc[2] = imsi[4];
  148. } else if (mnc_len == 3) {
  149. mnc[0] = imsi[3];
  150. mnc[1] = imsi[4];
  151. mnc[2] = imsi[5];
  152. }
  153. mnc[3] = '\0';
  154. pos = imsi + *imsi_len;
  155. pos += os_snprintf(pos, imsi + max_len - pos,
  156. "@wlan.mnc%s.mcc%c%c%c.3gppnetwork.org",
  157. mnc, imsi[0], imsi[1], imsi[2]);
  158. *imsi_len = pos - imsi;
  159. return 0;
  160. }
  161. #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
  162. /*
  163. * This state initializes state machine variables when the machine is
  164. * activated (portEnabled = TRUE). This is also used when re-starting
  165. * authentication (eapRestart == TRUE).
  166. */
  167. SM_STATE(EAP, INITIALIZE)
  168. {
  169. SM_ENTRY(EAP, INITIALIZE);
  170. if (sm->fast_reauth && sm->m && sm->m->has_reauth_data &&
  171. sm->m->has_reauth_data(sm, sm->eap_method_priv) &&
  172. !sm->prev_failure &&
  173. sm->last_config == eap_get_config(sm)) {
  174. wpa_printf(MSG_DEBUG, "EAP: maintaining EAP method data for "
  175. "fast reauthentication");
  176. sm->m->deinit_for_reauth(sm, sm->eap_method_priv);
  177. } else {
  178. sm->last_config = eap_get_config(sm);
  179. eap_deinit_prev_method(sm, "INITIALIZE");
  180. }
  181. sm->selectedMethod = EAP_TYPE_NONE;
  182. sm->methodState = METHOD_NONE;
  183. sm->allowNotifications = TRUE;
  184. sm->decision = DECISION_FAIL;
  185. sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
  186. eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
  187. eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
  188. eapol_set_bool(sm, EAPOL_eapFail, FALSE);
  189. eap_sm_free_key(sm);
  190. os_free(sm->eapSessionId);
  191. sm->eapSessionId = NULL;
  192. sm->eapKeyAvailable = FALSE;
  193. eapol_set_bool(sm, EAPOL_eapRestart, FALSE);
  194. sm->lastId = -1; /* new session - make sure this does not match with
  195. * the first EAP-Packet */
  196. /*
  197. * RFC 4137 does not reset eapResp and eapNoResp here. However, this
  198. * seemed to be able to trigger cases where both were set and if EAPOL
  199. * state machine uses eapNoResp first, it may end up not sending a real
  200. * reply correctly. This occurred when the workaround in FAIL state set
  201. * eapNoResp = TRUE.. Maybe that workaround needs to be fixed to do
  202. * something else(?)
  203. */
  204. eapol_set_bool(sm, EAPOL_eapResp, FALSE);
  205. eapol_set_bool(sm, EAPOL_eapNoResp, FALSE);
  206. /*
  207. * RFC 4137 does not reset ignore here, but since it is possible for
  208. * some method code paths to end up not setting ignore=FALSE, clear the
  209. * value here to avoid issues if a previous authentication attempt
  210. * failed with ignore=TRUE being left behind in the last
  211. * m.check(eapReqData) operation.
  212. */
  213. sm->ignore = 0;
  214. sm->num_rounds = 0;
  215. sm->prev_failure = 0;
  216. sm->expected_failure = 0;
  217. sm->reauthInit = FALSE;
  218. sm->erp_seq = (u32) -1;
  219. }
  220. /*
  221. * This state is reached whenever service from the lower layer is interrupted
  222. * or unavailable (portEnabled == FALSE). Immediate transition to INITIALIZE
  223. * occurs when the port becomes enabled.
  224. */
  225. SM_STATE(EAP, DISABLED)
  226. {
  227. SM_ENTRY(EAP, DISABLED);
  228. sm->num_rounds = 0;
  229. /*
  230. * RFC 4137 does not describe clearing of idleWhile here, but doing so
  231. * allows the timer tick to be stopped more quickly when EAP is not in
  232. * use.
  233. */
  234. eapol_set_int(sm, EAPOL_idleWhile, 0);
  235. }
  236. /*
  237. * The state machine spends most of its time here, waiting for something to
  238. * happen. This state is entered unconditionally from INITIALIZE, DISCARD, and
  239. * SEND_RESPONSE states.
  240. */
  241. SM_STATE(EAP, IDLE)
  242. {
  243. SM_ENTRY(EAP, IDLE);
  244. }
  245. /*
  246. * This state is entered when an EAP packet is received (eapReq == TRUE) to
  247. * parse the packet header.
  248. */
  249. SM_STATE(EAP, RECEIVED)
  250. {
  251. const struct wpabuf *eapReqData;
  252. SM_ENTRY(EAP, RECEIVED);
  253. eapReqData = eapol_get_eapReqData(sm);
  254. /* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */
  255. eap_sm_parseEapReq(sm, eapReqData);
  256. sm->num_rounds++;
  257. }
  258. /*
  259. * This state is entered when a request for a new type comes in. Either the
  260. * correct method is started, or a Nak response is built.
  261. */
  262. SM_STATE(EAP, GET_METHOD)
  263. {
  264. int reinit;
  265. EapType method;
  266. const struct eap_method *eap_method;
  267. SM_ENTRY(EAP, GET_METHOD);
  268. if (sm->reqMethod == EAP_TYPE_EXPANDED)
  269. method = sm->reqVendorMethod;
  270. else
  271. method = sm->reqMethod;
  272. eap_method = eap_peer_get_eap_method(sm->reqVendor, method);
  273. if (!eap_sm_allowMethod(sm, sm->reqVendor, method)) {
  274. wpa_printf(MSG_DEBUG, "EAP: vendor %u method %u not allowed",
  275. sm->reqVendor, method);
  276. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
  277. "vendor=%u method=%u -> NAK",
  278. sm->reqVendor, method);
  279. eap_notify_status(sm, "refuse proposed method",
  280. eap_method ? eap_method->name : "unknown");
  281. goto nak;
  282. }
  283. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
  284. "vendor=%u method=%u", sm->reqVendor, method);
  285. eap_notify_status(sm, "accept proposed method",
  286. eap_method ? eap_method->name : "unknown");
  287. /*
  288. * RFC 4137 does not define specific operation for fast
  289. * re-authentication (session resumption). The design here is to allow
  290. * the previously used method data to be maintained for
  291. * re-authentication if the method support session resumption.
  292. * Otherwise, the previously used method data is freed and a new method
  293. * is allocated here.
  294. */
  295. if (sm->fast_reauth &&
  296. sm->m && sm->m->vendor == sm->reqVendor &&
  297. sm->m->method == method &&
  298. sm->m->has_reauth_data &&
  299. sm->m->has_reauth_data(sm, sm->eap_method_priv)) {
  300. wpa_printf(MSG_DEBUG, "EAP: Using previous method data"
  301. " for fast re-authentication");
  302. reinit = 1;
  303. } else {
  304. eap_deinit_prev_method(sm, "GET_METHOD");
  305. reinit = 0;
  306. }
  307. sm->selectedMethod = sm->reqMethod;
  308. if (sm->m == NULL)
  309. sm->m = eap_method;
  310. if (!sm->m) {
  311. wpa_printf(MSG_DEBUG, "EAP: Could not find selected method: "
  312. "vendor %d method %d",
  313. sm->reqVendor, method);
  314. goto nak;
  315. }
  316. sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
  317. wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP method: "
  318. "vendor %u method %u (%s)",
  319. sm->reqVendor, method, sm->m->name);
  320. if (reinit) {
  321. sm->eap_method_priv = sm->m->init_for_reauth(
  322. sm, sm->eap_method_priv);
  323. } else {
  324. sm->waiting_ext_cert_check = 0;
  325. sm->ext_cert_check = 0;
  326. sm->eap_method_priv = sm->m->init(sm);
  327. }
  328. if (sm->eap_method_priv == NULL) {
  329. struct eap_peer_config *config = eap_get_config(sm);
  330. wpa_msg(sm->msg_ctx, MSG_INFO,
  331. "EAP: Failed to initialize EAP method: vendor %u "
  332. "method %u (%s)",
  333. sm->reqVendor, method, sm->m->name);
  334. sm->m = NULL;
  335. sm->methodState = METHOD_NONE;
  336. sm->selectedMethod = EAP_TYPE_NONE;
  337. if (sm->reqMethod == EAP_TYPE_TLS && config &&
  338. (config->pending_req_pin ||
  339. config->pending_req_passphrase)) {
  340. /*
  341. * Return without generating Nak in order to allow
  342. * entering of PIN code or passphrase to retry the
  343. * current EAP packet.
  344. */
  345. wpa_printf(MSG_DEBUG, "EAP: Pending PIN/passphrase "
  346. "request - skip Nak");
  347. return;
  348. }
  349. goto nak;
  350. }
  351. sm->methodState = METHOD_INIT;
  352. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_METHOD
  353. "EAP vendor %u method %u (%s) selected",
  354. sm->reqVendor, method, sm->m->name);
  355. return;
  356. nak:
  357. wpabuf_free(sm->eapRespData);
  358. sm->eapRespData = NULL;
  359. sm->eapRespData = eap_sm_buildNak(sm, sm->reqId);
  360. }
  361. #ifdef CONFIG_ERP
  362. static char * eap_get_realm(struct eap_sm *sm, struct eap_peer_config *config)
  363. {
  364. char *realm;
  365. size_t i, realm_len;
  366. if (!config)
  367. return NULL;
  368. if (config->identity) {
  369. for (i = 0; i < config->identity_len; i++) {
  370. if (config->identity[i] == '@')
  371. break;
  372. }
  373. if (i < config->identity_len) {
  374. realm_len = config->identity_len - i - 1;
  375. realm = os_malloc(realm_len + 1);
  376. if (realm == NULL)
  377. return NULL;
  378. os_memcpy(realm, &config->identity[i + 1], realm_len);
  379. realm[realm_len] = '\0';
  380. return realm;
  381. }
  382. }
  383. if (config->anonymous_identity) {
  384. for (i = 0; i < config->anonymous_identity_len; i++) {
  385. if (config->anonymous_identity[i] == '@')
  386. break;
  387. }
  388. if (i < config->anonymous_identity_len) {
  389. realm_len = config->anonymous_identity_len - i - 1;
  390. realm = os_malloc(realm_len + 1);
  391. if (realm == NULL)
  392. return NULL;
  393. os_memcpy(realm, &config->anonymous_identity[i + 1],
  394. realm_len);
  395. realm[realm_len] = '\0';
  396. return realm;
  397. }
  398. }
  399. #ifdef CONFIG_EAP_PROXY
  400. /* When identity is not provided in the config, build the realm from
  401. * IMSI for eap_proxy based methods.
  402. */
  403. if (!config->identity && !config->anonymous_identity &&
  404. sm->eapol_cb->get_imsi &&
  405. (eap_config_allowed_method(sm, config, EAP_VENDOR_IETF,
  406. EAP_TYPE_SIM) ||
  407. eap_config_allowed_method(sm, config, EAP_VENDOR_IETF,
  408. EAP_TYPE_AKA) ||
  409. eap_config_allowed_method(sm, config, EAP_VENDOR_IETF,
  410. EAP_TYPE_AKA_PRIME))) {
  411. char imsi[100];
  412. size_t imsi_len;
  413. int mnc_len, pos;
  414. wpa_printf(MSG_DEBUG, "EAP: Build realm from IMSI (eap_proxy)");
  415. mnc_len = sm->eapol_cb->get_imsi(sm->eapol_ctx, config->sim_num,
  416. imsi, &imsi_len);
  417. if (mnc_len < 0)
  418. return NULL;
  419. pos = imsi_len + 1; /* points to the beginning of the realm */
  420. if (eap_sm_append_3gpp_realm(sm, imsi, sizeof(imsi), &imsi_len,
  421. mnc_len) < 0) {
  422. wpa_printf(MSG_WARNING, "Could not append realm");
  423. return NULL;
  424. }
  425. realm = os_strdup(&imsi[pos]);
  426. if (!realm)
  427. return NULL;
  428. wpa_printf(MSG_DEBUG, "EAP: Generated realm '%s'", realm);
  429. return realm;
  430. }
  431. #endif /* CONFIG_EAP_PROXY */
  432. return NULL;
  433. }
  434. static char * eap_home_realm(struct eap_sm *sm)
  435. {
  436. return eap_get_realm(sm, eap_get_config(sm));
  437. }
  438. static struct eap_erp_key *
  439. eap_erp_get_key(struct eap_sm *sm, const char *realm)
  440. {
  441. struct eap_erp_key *erp;
  442. dl_list_for_each(erp, &sm->erp_keys, struct eap_erp_key, list) {
  443. char *pos;
  444. pos = os_strchr(erp->keyname_nai, '@');
  445. if (!pos)
  446. continue;
  447. pos++;
  448. if (os_strcmp(pos, realm) == 0)
  449. return erp;
  450. }
  451. return NULL;
  452. }
  453. static struct eap_erp_key *
  454. eap_erp_get_key_nai(struct eap_sm *sm, const char *nai)
  455. {
  456. struct eap_erp_key *erp;
  457. dl_list_for_each(erp, &sm->erp_keys, struct eap_erp_key, list) {
  458. if (os_strcmp(erp->keyname_nai, nai) == 0)
  459. return erp;
  460. }
  461. return NULL;
  462. }
  463. static void eap_peer_erp_free_key(struct eap_erp_key *erp)
  464. {
  465. dl_list_del(&erp->list);
  466. bin_clear_free(erp, sizeof(*erp));
  467. }
  468. static void eap_erp_remove_keys_realm(struct eap_sm *sm, const char *realm)
  469. {
  470. struct eap_erp_key *erp;
  471. while ((erp = eap_erp_get_key(sm, realm)) != NULL) {
  472. wpa_printf(MSG_DEBUG, "EAP: Delete old ERP key %s",
  473. erp->keyname_nai);
  474. eap_peer_erp_free_key(erp);
  475. }
  476. }
  477. int eap_peer_update_erp_next_seq_num(struct eap_sm *sm, u16 next_seq_num)
  478. {
  479. struct eap_erp_key *erp;
  480. char *home_realm;
  481. home_realm = eap_home_realm(sm);
  482. if (!home_realm || os_strlen(home_realm) == 0) {
  483. os_free(home_realm);
  484. return -1;
  485. }
  486. erp = eap_erp_get_key(sm, home_realm);
  487. if (!erp) {
  488. wpa_printf(MSG_DEBUG,
  489. "EAP: Failed to find ERP key for realm: %s",
  490. home_realm);
  491. os_free(home_realm);
  492. return -1;
  493. }
  494. if ((u32) next_seq_num < erp->next_seq) {
  495. /* Sequence number has wrapped around, clear this ERP
  496. * info and do a full auth next time.
  497. */
  498. eap_peer_erp_free_key(erp);
  499. } else {
  500. erp->next_seq = (u32) next_seq_num;
  501. }
  502. os_free(home_realm);
  503. return 0;
  504. }
  505. int eap_peer_get_erp_info(struct eap_sm *sm, struct eap_peer_config *config,
  506. const u8 **username, size_t *username_len,
  507. const u8 **realm, size_t *realm_len,
  508. u16 *erp_next_seq_num, const u8 **rrk,
  509. size_t *rrk_len)
  510. {
  511. struct eap_erp_key *erp;
  512. char *home_realm;
  513. char *pos;
  514. home_realm = eap_get_realm(sm, config);
  515. if (!home_realm || os_strlen(home_realm) == 0) {
  516. os_free(home_realm);
  517. return -1;
  518. }
  519. erp = eap_erp_get_key(sm, home_realm);
  520. os_free(home_realm);
  521. if (!erp)
  522. return -1;
  523. if (erp->next_seq >= 65536)
  524. return -1; /* SEQ has range of 0..65535 */
  525. pos = os_strchr(erp->keyname_nai, '@');
  526. if (!pos)
  527. return -1; /* this cannot really happen */
  528. *username_len = pos - erp->keyname_nai;
  529. *username = (u8 *) erp->keyname_nai;
  530. pos++;
  531. *realm_len = os_strlen(pos);
  532. *realm = (u8 *) pos;
  533. *erp_next_seq_num = (u16) erp->next_seq;
  534. *rrk_len = erp->rRK_len;
  535. *rrk = erp->rRK;
  536. if (*username_len == 0 || *realm_len == 0 || *rrk_len == 0)
  537. return -1;
  538. return 0;
  539. }
  540. #endif /* CONFIG_ERP */
  541. void eap_peer_erp_free_keys(struct eap_sm *sm)
  542. {
  543. #ifdef CONFIG_ERP
  544. struct eap_erp_key *erp, *tmp;
  545. dl_list_for_each_safe(erp, tmp, &sm->erp_keys, struct eap_erp_key, list)
  546. eap_peer_erp_free_key(erp);
  547. #endif /* CONFIG_ERP */
  548. }
  549. void eap_peer_erp_init(struct eap_sm *sm, u8 *ext_session_id,
  550. size_t ext_session_id_len, u8 *ext_emsk,
  551. size_t ext_emsk_len)
  552. {
  553. #ifdef CONFIG_ERP
  554. u8 *emsk = NULL;
  555. size_t emsk_len = 0;
  556. u8 *session_id = NULL;
  557. size_t session_id_len = 0;
  558. u8 EMSKname[EAP_EMSK_NAME_LEN];
  559. u8 len[2], ctx[3];
  560. char *realm;
  561. size_t realm_len, nai_buf_len;
  562. struct eap_erp_key *erp = NULL;
  563. int pos;
  564. realm = eap_home_realm(sm);
  565. if (!realm)
  566. return;
  567. realm_len = os_strlen(realm);
  568. wpa_printf(MSG_DEBUG, "EAP: Realm for ERP keyName-NAI: %s", realm);
  569. eap_erp_remove_keys_realm(sm, realm);
  570. nai_buf_len = 2 * EAP_EMSK_NAME_LEN + 1 + realm_len;
  571. if (nai_buf_len > 253) {
  572. /*
  573. * keyName-NAI has a maximum length of 253 octet to fit in
  574. * RADIUS attributes.
  575. */
  576. wpa_printf(MSG_DEBUG,
  577. "EAP: Too long realm for ERP keyName-NAI maximum length");
  578. goto fail;
  579. }
  580. nai_buf_len++; /* null termination */
  581. erp = os_zalloc(sizeof(*erp) + nai_buf_len);
  582. if (erp == NULL)
  583. goto fail;
  584. if (ext_emsk) {
  585. emsk = ext_emsk;
  586. emsk_len = ext_emsk_len;
  587. } else {
  588. emsk = sm->m->get_emsk(sm, sm->eap_method_priv, &emsk_len);
  589. }
  590. if (!emsk || emsk_len == 0 || emsk_len > ERP_MAX_KEY_LEN) {
  591. wpa_printf(MSG_DEBUG,
  592. "EAP: No suitable EMSK available for ERP");
  593. goto fail;
  594. }
  595. wpa_hexdump_key(MSG_DEBUG, "EAP: EMSK", emsk, emsk_len);
  596. if (ext_session_id) {
  597. session_id = ext_session_id;
  598. session_id_len = ext_session_id_len;
  599. } else {
  600. session_id = sm->eapSessionId;
  601. session_id_len = sm->eapSessionIdLen;
  602. }
  603. if (!session_id || session_id_len == 0) {
  604. wpa_printf(MSG_DEBUG,
  605. "EAP: No suitable session id available for ERP");
  606. goto fail;
  607. }
  608. WPA_PUT_BE16(len, EAP_EMSK_NAME_LEN);
  609. if (hmac_sha256_kdf(session_id, session_id_len, "EMSK", len,
  610. sizeof(len), EMSKname, EAP_EMSK_NAME_LEN) < 0) {
  611. wpa_printf(MSG_DEBUG, "EAP: Could not derive EMSKname");
  612. goto fail;
  613. }
  614. wpa_hexdump(MSG_DEBUG, "EAP: EMSKname", EMSKname, EAP_EMSK_NAME_LEN);
  615. pos = wpa_snprintf_hex(erp->keyname_nai, nai_buf_len,
  616. EMSKname, EAP_EMSK_NAME_LEN);
  617. erp->keyname_nai[pos] = '@';
  618. os_memcpy(&erp->keyname_nai[pos + 1], realm, realm_len);
  619. WPA_PUT_BE16(len, emsk_len);
  620. if (hmac_sha256_kdf(emsk, emsk_len,
  621. "EAP Re-authentication Root Key@ietf.org",
  622. len, sizeof(len), erp->rRK, emsk_len) < 0) {
  623. wpa_printf(MSG_DEBUG, "EAP: Could not derive rRK for ERP");
  624. goto fail;
  625. }
  626. erp->rRK_len = emsk_len;
  627. wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rRK", erp->rRK, erp->rRK_len);
  628. ctx[0] = EAP_ERP_CS_HMAC_SHA256_128;
  629. WPA_PUT_BE16(&ctx[1], erp->rRK_len);
  630. if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
  631. "Re-authentication Integrity Key@ietf.org",
  632. ctx, sizeof(ctx), erp->rIK, erp->rRK_len) < 0) {
  633. wpa_printf(MSG_DEBUG, "EAP: Could not derive rIK for ERP");
  634. goto fail;
  635. }
  636. erp->rIK_len = erp->rRK_len;
  637. wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rIK", erp->rIK, erp->rIK_len);
  638. wpa_printf(MSG_DEBUG, "EAP: Stored ERP keys %s", erp->keyname_nai);
  639. dl_list_add(&sm->erp_keys, &erp->list);
  640. erp = NULL;
  641. fail:
  642. bin_clear_free(emsk, emsk_len);
  643. bin_clear_free(ext_session_id, ext_session_id_len);
  644. bin_clear_free(erp, sizeof(*erp));
  645. os_free(realm);
  646. #endif /* CONFIG_ERP */
  647. }
  648. #ifdef CONFIG_ERP
  649. struct wpabuf * eap_peer_build_erp_reauth_start(struct eap_sm *sm, u8 eap_id)
  650. {
  651. char *realm;
  652. struct eap_erp_key *erp;
  653. struct wpabuf *msg;
  654. u8 hash[SHA256_MAC_LEN];
  655. realm = eap_home_realm(sm);
  656. if (!realm)
  657. return NULL;
  658. erp = eap_erp_get_key(sm, realm);
  659. os_free(realm);
  660. realm = NULL;
  661. if (!erp)
  662. return NULL;
  663. if (erp->next_seq >= 65536)
  664. return NULL; /* SEQ has range of 0..65535 */
  665. /* TODO: check rRK lifetime expiration */
  666. wpa_printf(MSG_DEBUG, "EAP: Valid ERP key found %s (SEQ=%u)",
  667. erp->keyname_nai, erp->next_seq);
  668. msg = eap_msg_alloc(EAP_VENDOR_IETF, (EapType) EAP_ERP_TYPE_REAUTH,
  669. 1 + 2 + 2 + os_strlen(erp->keyname_nai) + 1 + 16,
  670. EAP_CODE_INITIATE, eap_id);
  671. if (msg == NULL)
  672. return NULL;
  673. wpabuf_put_u8(msg, 0x20); /* Flags: R=0 B=0 L=1 */
  674. wpabuf_put_be16(msg, erp->next_seq);
  675. wpabuf_put_u8(msg, EAP_ERP_TLV_KEYNAME_NAI);
  676. wpabuf_put_u8(msg, os_strlen(erp->keyname_nai));
  677. wpabuf_put_str(msg, erp->keyname_nai);
  678. wpabuf_put_u8(msg, EAP_ERP_CS_HMAC_SHA256_128); /* Cryptosuite */
  679. if (hmac_sha256(erp->rIK, erp->rIK_len,
  680. wpabuf_head(msg), wpabuf_len(msg), hash) < 0) {
  681. wpabuf_free(msg);
  682. return NULL;
  683. }
  684. wpabuf_put_data(msg, hash, 16);
  685. sm->erp_seq = erp->next_seq;
  686. erp->next_seq++;
  687. wpa_hexdump_buf(MSG_DEBUG, "ERP: EAP-Initiate/Re-auth", msg);
  688. return msg;
  689. }
  690. static int eap_peer_erp_reauth_start(struct eap_sm *sm, u8 eap_id)
  691. {
  692. struct wpabuf *msg;
  693. msg = eap_peer_build_erp_reauth_start(sm, eap_id);
  694. if (!msg)
  695. return -1;
  696. wpa_printf(MSG_DEBUG, "EAP: Sending EAP-Initiate/Re-auth");
  697. wpabuf_free(sm->eapRespData);
  698. sm->eapRespData = msg;
  699. sm->reauthInit = TRUE;
  700. return 0;
  701. }
  702. #endif /* CONFIG_ERP */
  703. /*
  704. * The method processing happens here. The request from the authenticator is
  705. * processed, and an appropriate response packet is built.
  706. */
  707. SM_STATE(EAP, METHOD)
  708. {
  709. struct wpabuf *eapReqData;
  710. struct eap_method_ret ret;
  711. int min_len = 1;
  712. SM_ENTRY(EAP, METHOD);
  713. if (sm->m == NULL) {
  714. wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected");
  715. return;
  716. }
  717. eapReqData = eapol_get_eapReqData(sm);
  718. if (sm->m->vendor == EAP_VENDOR_IETF && sm->m->method == EAP_TYPE_LEAP)
  719. min_len = 0; /* LEAP uses EAP-Success without payload */
  720. if (!eap_hdr_len_valid(eapReqData, min_len))
  721. return;
  722. /*
  723. * Get ignore, methodState, decision, allowNotifications, and
  724. * eapRespData. RFC 4137 uses three separate method procedure (check,
  725. * process, and buildResp) in this state. These have been combined into
  726. * a single function call to m->process() in order to optimize EAP
  727. * method implementation interface a bit. These procedures are only
  728. * used from within this METHOD state, so there is no need to keep
  729. * these as separate C functions.
  730. *
  731. * The RFC 4137 procedures return values as follows:
  732. * ignore = m.check(eapReqData)
  733. * (methodState, decision, allowNotifications) = m.process(eapReqData)
  734. * eapRespData = m.buildResp(reqId)
  735. */
  736. os_memset(&ret, 0, sizeof(ret));
  737. ret.ignore = sm->ignore;
  738. ret.methodState = sm->methodState;
  739. ret.decision = sm->decision;
  740. ret.allowNotifications = sm->allowNotifications;
  741. wpabuf_free(sm->eapRespData);
  742. sm->eapRespData = NULL;
  743. sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret,
  744. eapReqData);
  745. wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s "
  746. "methodState=%s decision=%s eapRespData=%p",
  747. ret.ignore ? "TRUE" : "FALSE",
  748. eap_sm_method_state_txt(ret.methodState),
  749. eap_sm_decision_txt(ret.decision),
  750. sm->eapRespData);
  751. sm->ignore = ret.ignore;
  752. if (sm->ignore)
  753. return;
  754. sm->methodState = ret.methodState;
  755. sm->decision = ret.decision;
  756. sm->allowNotifications = ret.allowNotifications;
  757. if (sm->m->isKeyAvailable && sm->m->getKey &&
  758. sm->m->isKeyAvailable(sm, sm->eap_method_priv)) {
  759. struct eap_peer_config *config = eap_get_config(sm);
  760. eap_sm_free_key(sm);
  761. sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv,
  762. &sm->eapKeyDataLen);
  763. os_free(sm->eapSessionId);
  764. sm->eapSessionId = NULL;
  765. if (sm->m->getSessionId) {
  766. sm->eapSessionId = sm->m->getSessionId(
  767. sm, sm->eap_method_priv,
  768. &sm->eapSessionIdLen);
  769. wpa_hexdump(MSG_DEBUG, "EAP: Session-Id",
  770. sm->eapSessionId, sm->eapSessionIdLen);
  771. }
  772. if (config->erp && sm->m->get_emsk && sm->eapSessionId)
  773. eap_peer_erp_init(sm, NULL, 0, NULL, 0);
  774. }
  775. }
  776. /*
  777. * This state signals the lower layer that a response packet is ready to be
  778. * sent.
  779. */
  780. SM_STATE(EAP, SEND_RESPONSE)
  781. {
  782. SM_ENTRY(EAP, SEND_RESPONSE);
  783. wpabuf_free(sm->lastRespData);
  784. if (sm->eapRespData) {
  785. if (sm->workaround)
  786. os_memcpy(sm->last_sha1, sm->req_sha1, 20);
  787. sm->lastId = sm->reqId;
  788. sm->lastRespData = wpabuf_dup(sm->eapRespData);
  789. eapol_set_bool(sm, EAPOL_eapResp, TRUE);
  790. } else {
  791. wpa_printf(MSG_DEBUG, "EAP: No eapRespData available");
  792. sm->lastRespData = NULL;
  793. }
  794. eapol_set_bool(sm, EAPOL_eapReq, FALSE);
  795. eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
  796. sm->reauthInit = FALSE;
  797. }
  798. /*
  799. * This state signals the lower layer that the request was discarded, and no
  800. * response packet will be sent at this time.
  801. */
  802. SM_STATE(EAP, DISCARD)
  803. {
  804. SM_ENTRY(EAP, DISCARD);
  805. eapol_set_bool(sm, EAPOL_eapReq, FALSE);
  806. eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
  807. }
  808. /*
  809. * Handles requests for Identity method and builds a response.
  810. */
  811. SM_STATE(EAP, IDENTITY)
  812. {
  813. const struct wpabuf *eapReqData;
  814. SM_ENTRY(EAP, IDENTITY);
  815. eapReqData = eapol_get_eapReqData(sm);
  816. if (!eap_hdr_len_valid(eapReqData, 1))
  817. return;
  818. eap_sm_processIdentity(sm, eapReqData);
  819. wpabuf_free(sm->eapRespData);
  820. sm->eapRespData = NULL;
  821. sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId, 0);
  822. }
  823. /*
  824. * Handles requests for Notification method and builds a response.
  825. */
  826. SM_STATE(EAP, NOTIFICATION)
  827. {
  828. const struct wpabuf *eapReqData;
  829. SM_ENTRY(EAP, NOTIFICATION);
  830. eapReqData = eapol_get_eapReqData(sm);
  831. if (!eap_hdr_len_valid(eapReqData, 1))
  832. return;
  833. eap_sm_processNotify(sm, eapReqData);
  834. wpabuf_free(sm->eapRespData);
  835. sm->eapRespData = NULL;
  836. sm->eapRespData = eap_sm_buildNotify(sm->reqId);
  837. }
  838. /*
  839. * This state retransmits the previous response packet.
  840. */
  841. SM_STATE(EAP, RETRANSMIT)
  842. {
  843. SM_ENTRY(EAP, RETRANSMIT);
  844. wpabuf_free(sm->eapRespData);
  845. if (sm->lastRespData)
  846. sm->eapRespData = wpabuf_dup(sm->lastRespData);
  847. else
  848. sm->eapRespData = NULL;
  849. }
  850. /*
  851. * This state is entered in case of a successful completion of authentication
  852. * and state machine waits here until port is disabled or EAP authentication is
  853. * restarted.
  854. */
  855. SM_STATE(EAP, SUCCESS)
  856. {
  857. SM_ENTRY(EAP, SUCCESS);
  858. if (sm->eapKeyData != NULL)
  859. sm->eapKeyAvailable = TRUE;
  860. eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
  861. /*
  862. * RFC 4137 does not clear eapReq here, but this seems to be required
  863. * to avoid processing the same request twice when state machine is
  864. * initialized.
  865. */
  866. eapol_set_bool(sm, EAPOL_eapReq, FALSE);
  867. /*
  868. * RFC 4137 does not set eapNoResp here, but this seems to be required
  869. * to get EAPOL Supplicant backend state machine into SUCCESS state. In
  870. * addition, either eapResp or eapNoResp is required to be set after
  871. * processing the received EAP frame.
  872. */
  873. eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
  874. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
  875. "EAP authentication completed successfully");
  876. }
  877. /*
  878. * This state is entered in case of a failure and state machine waits here
  879. * until port is disabled or EAP authentication is restarted.
  880. */
  881. SM_STATE(EAP, FAILURE)
  882. {
  883. SM_ENTRY(EAP, FAILURE);
  884. eapol_set_bool(sm, EAPOL_eapFail, TRUE);
  885. /*
  886. * RFC 4137 does not clear eapReq here, but this seems to be required
  887. * to avoid processing the same request twice when state machine is
  888. * initialized.
  889. */
  890. eapol_set_bool(sm, EAPOL_eapReq, FALSE);
  891. /*
  892. * RFC 4137 does not set eapNoResp here. However, either eapResp or
  893. * eapNoResp is required to be set after processing the received EAP
  894. * frame.
  895. */
  896. eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
  897. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
  898. "EAP authentication failed");
  899. sm->prev_failure = 1;
  900. }
  901. static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId)
  902. {
  903. /*
  904. * At least Microsoft IAS and Meetinghouse Aegis seem to be sending
  905. * EAP-Success/Failure with lastId + 1 even though RFC 3748 and
  906. * RFC 4137 require that reqId == lastId. In addition, it looks like
  907. * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
  908. *
  909. * Accept this kind of Id if EAP workarounds are enabled. These are
  910. * unauthenticated plaintext messages, so this should have minimal
  911. * security implications (bit easier to fake EAP-Success/Failure).
  912. */
  913. if (sm->workaround && (reqId == ((lastId + 1) & 0xff) ||
  914. reqId == ((lastId + 2) & 0xff))) {
  915. wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected "
  916. "identifier field in EAP Success: "
  917. "reqId=%d lastId=%d (these are supposed to be "
  918. "same)", reqId, lastId);
  919. return 1;
  920. }
  921. wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d "
  922. "lastId=%d", reqId, lastId);
  923. return 0;
  924. }
  925. /*
  926. * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
  927. */
  928. static void eap_peer_sm_step_idle(struct eap_sm *sm)
  929. {
  930. /*
  931. * The first three transitions are from RFC 4137. The last two are
  932. * local additions to handle special cases with LEAP and PEAP server
  933. * not sending EAP-Success in some cases.
  934. */
  935. if (eapol_get_bool(sm, EAPOL_eapReq))
  936. SM_ENTER(EAP, RECEIVED);
  937. else if ((eapol_get_bool(sm, EAPOL_altAccept) &&
  938. sm->decision != DECISION_FAIL) ||
  939. (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
  940. sm->decision == DECISION_UNCOND_SUCC))
  941. SM_ENTER(EAP, SUCCESS);
  942. else if (eapol_get_bool(sm, EAPOL_altReject) ||
  943. (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
  944. sm->decision != DECISION_UNCOND_SUCC) ||
  945. (eapol_get_bool(sm, EAPOL_altAccept) &&
  946. sm->methodState != METHOD_CONT &&
  947. sm->decision == DECISION_FAIL))
  948. SM_ENTER(EAP, FAILURE);
  949. else if (sm->selectedMethod == EAP_TYPE_LEAP &&
  950. sm->leap_done && sm->decision != DECISION_FAIL &&
  951. sm->methodState == METHOD_DONE)
  952. SM_ENTER(EAP, SUCCESS);
  953. else if (sm->selectedMethod == EAP_TYPE_PEAP &&
  954. sm->peap_done && sm->decision != DECISION_FAIL &&
  955. sm->methodState == METHOD_DONE)
  956. SM_ENTER(EAP, SUCCESS);
  957. }
  958. static int eap_peer_req_is_duplicate(struct eap_sm *sm)
  959. {
  960. int duplicate;
  961. duplicate = (sm->reqId == sm->lastId) && sm->rxReq;
  962. if (sm->workaround && duplicate &&
  963. os_memcmp(sm->req_sha1, sm->last_sha1, 20) != 0) {
  964. /*
  965. * RFC 4137 uses (reqId == lastId) as the only verification for
  966. * duplicate EAP requests. However, this misses cases where the
  967. * AS is incorrectly using the same id again; and
  968. * unfortunately, such implementations exist. Use SHA1 hash as
  969. * an extra verification for the packets being duplicate to
  970. * workaround these issues.
  971. */
  972. wpa_printf(MSG_DEBUG, "EAP: AS used the same Id again, but "
  973. "EAP packets were not identical");
  974. wpa_printf(MSG_DEBUG, "EAP: workaround - assume this is not a "
  975. "duplicate packet");
  976. duplicate = 0;
  977. }
  978. return duplicate;
  979. }
  980. static int eap_peer_sm_allow_canned(struct eap_sm *sm)
  981. {
  982. struct eap_peer_config *config = eap_get_config(sm);
  983. return config && config->phase1 &&
  984. os_strstr(config->phase1, "allow_canned_success=1");
  985. }
  986. static void eap_peer_sm_step_received(struct eap_sm *sm)
  987. {
  988. int duplicate = eap_peer_req_is_duplicate(sm);
  989. /*
  990. * Two special cases below for LEAP are local additions to work around
  991. * odd LEAP behavior (EAP-Success in the middle of authentication and
  992. * then swapped roles). Other transitions are based on RFC 4137.
  993. */
  994. if (sm->rxSuccess && sm->decision != DECISION_FAIL &&
  995. (sm->reqId == sm->lastId ||
  996. eap_success_workaround(sm, sm->reqId, sm->lastId)))
  997. SM_ENTER(EAP, SUCCESS);
  998. else if (sm->workaround && sm->lastId == -1 && sm->rxSuccess &&
  999. !sm->rxFailure && !sm->rxReq && eap_peer_sm_allow_canned(sm))
  1000. SM_ENTER(EAP, SUCCESS); /* EAP-Success prior any EAP method */
  1001. else if (sm->workaround && sm->lastId == -1 && sm->rxFailure &&
  1002. !sm->rxReq && sm->methodState != METHOD_CONT &&
  1003. eap_peer_sm_allow_canned(sm))
  1004. SM_ENTER(EAP, FAILURE); /* EAP-Failure prior any EAP method */
  1005. else if (sm->workaround && sm->rxSuccess && !sm->rxFailure &&
  1006. !sm->rxReq && sm->methodState != METHOD_CONT &&
  1007. eap_peer_sm_allow_canned(sm))
  1008. SM_ENTER(EAP, SUCCESS); /* EAP-Success after Identity */
  1009. else if (sm->methodState != METHOD_CONT &&
  1010. ((sm->rxFailure &&
  1011. sm->decision != DECISION_UNCOND_SUCC) ||
  1012. (sm->rxSuccess && sm->decision == DECISION_FAIL &&
  1013. (sm->selectedMethod != EAP_TYPE_LEAP ||
  1014. sm->methodState != METHOD_MAY_CONT))) &&
  1015. (sm->reqId == sm->lastId ||
  1016. eap_success_workaround(sm, sm->reqId, sm->lastId)))
  1017. SM_ENTER(EAP, FAILURE);
  1018. else if (sm->rxReq && duplicate)
  1019. SM_ENTER(EAP, RETRANSMIT);
  1020. else if (sm->rxReq && !duplicate &&
  1021. sm->reqMethod == EAP_TYPE_NOTIFICATION &&
  1022. sm->allowNotifications)
  1023. SM_ENTER(EAP, NOTIFICATION);
  1024. else if (sm->rxReq && !duplicate &&
  1025. sm->selectedMethod == EAP_TYPE_NONE &&
  1026. sm->reqMethod == EAP_TYPE_IDENTITY)
  1027. SM_ENTER(EAP, IDENTITY);
  1028. else if (sm->rxReq && !duplicate &&
  1029. sm->selectedMethod == EAP_TYPE_NONE &&
  1030. sm->reqMethod != EAP_TYPE_IDENTITY &&
  1031. sm->reqMethod != EAP_TYPE_NOTIFICATION)
  1032. SM_ENTER(EAP, GET_METHOD);
  1033. else if (sm->rxReq && !duplicate &&
  1034. sm->reqMethod == sm->selectedMethod &&
  1035. sm->methodState != METHOD_DONE)
  1036. SM_ENTER(EAP, METHOD);
  1037. else if (sm->selectedMethod == EAP_TYPE_LEAP &&
  1038. (sm->rxSuccess || sm->rxResp))
  1039. SM_ENTER(EAP, METHOD);
  1040. else if (sm->reauthInit)
  1041. SM_ENTER(EAP, SEND_RESPONSE);
  1042. else
  1043. SM_ENTER(EAP, DISCARD);
  1044. }
  1045. static void eap_peer_sm_step_local(struct eap_sm *sm)
  1046. {
  1047. switch (sm->EAP_state) {
  1048. case EAP_INITIALIZE:
  1049. SM_ENTER(EAP, IDLE);
  1050. break;
  1051. case EAP_DISABLED:
  1052. if (eapol_get_bool(sm, EAPOL_portEnabled) &&
  1053. !sm->force_disabled)
  1054. SM_ENTER(EAP, INITIALIZE);
  1055. break;
  1056. case EAP_IDLE:
  1057. eap_peer_sm_step_idle(sm);
  1058. break;
  1059. case EAP_RECEIVED:
  1060. eap_peer_sm_step_received(sm);
  1061. break;
  1062. case EAP_GET_METHOD:
  1063. if (sm->selectedMethod == sm->reqMethod)
  1064. SM_ENTER(EAP, METHOD);
  1065. else
  1066. SM_ENTER(EAP, SEND_RESPONSE);
  1067. break;
  1068. case EAP_METHOD:
  1069. /*
  1070. * Note: RFC 4137 uses methodState == DONE && decision == FAIL
  1071. * as the condition. eapRespData == NULL here is used to allow
  1072. * final EAP method response to be sent without having to change
  1073. * all methods to either use methodState MAY_CONT or leaving
  1074. * decision to something else than FAIL in cases where the only
  1075. * expected response is EAP-Failure.
  1076. */
  1077. if (sm->ignore)
  1078. SM_ENTER(EAP, DISCARD);
  1079. else if (sm->methodState == METHOD_DONE &&
  1080. sm->decision == DECISION_FAIL && !sm->eapRespData)
  1081. SM_ENTER(EAP, FAILURE);
  1082. else
  1083. SM_ENTER(EAP, SEND_RESPONSE);
  1084. break;
  1085. case EAP_SEND_RESPONSE:
  1086. SM_ENTER(EAP, IDLE);
  1087. break;
  1088. case EAP_DISCARD:
  1089. SM_ENTER(EAP, IDLE);
  1090. break;
  1091. case EAP_IDENTITY:
  1092. SM_ENTER(EAP, SEND_RESPONSE);
  1093. break;
  1094. case EAP_NOTIFICATION:
  1095. SM_ENTER(EAP, SEND_RESPONSE);
  1096. break;
  1097. case EAP_RETRANSMIT:
  1098. SM_ENTER(EAP, SEND_RESPONSE);
  1099. break;
  1100. case EAP_SUCCESS:
  1101. break;
  1102. case EAP_FAILURE:
  1103. break;
  1104. }
  1105. }
  1106. SM_STEP(EAP)
  1107. {
  1108. /* Global transitions */
  1109. if (eapol_get_bool(sm, EAPOL_eapRestart) &&
  1110. eapol_get_bool(sm, EAPOL_portEnabled))
  1111. SM_ENTER_GLOBAL(EAP, INITIALIZE);
  1112. else if (!eapol_get_bool(sm, EAPOL_portEnabled) || sm->force_disabled)
  1113. SM_ENTER_GLOBAL(EAP, DISABLED);
  1114. else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
  1115. /* RFC 4137 does not place any limit on number of EAP messages
  1116. * in an authentication session. However, some error cases have
  1117. * ended up in a state were EAP messages were sent between the
  1118. * peer and server in a loop (e.g., TLS ACK frame in both
  1119. * direction). Since this is quite undesired outcome, limit the
  1120. * total number of EAP round-trips and abort authentication if
  1121. * this limit is exceeded.
  1122. */
  1123. if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
  1124. wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: more than %d "
  1125. "authentication rounds - abort",
  1126. EAP_MAX_AUTH_ROUNDS);
  1127. sm->num_rounds++;
  1128. SM_ENTER_GLOBAL(EAP, FAILURE);
  1129. }
  1130. } else {
  1131. /* Local transitions */
  1132. eap_peer_sm_step_local(sm);
  1133. }
  1134. }
  1135. static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
  1136. EapType method)
  1137. {
  1138. if (!eap_allowed_method(sm, vendor, method)) {
  1139. wpa_printf(MSG_DEBUG, "EAP: configuration does not allow: "
  1140. "vendor %u method %u", vendor, method);
  1141. return FALSE;
  1142. }
  1143. if (eap_peer_get_eap_method(vendor, method))
  1144. return TRUE;
  1145. wpa_printf(MSG_DEBUG, "EAP: not included in build: "
  1146. "vendor %u method %u", vendor, method);
  1147. return FALSE;
  1148. }
  1149. static struct wpabuf * eap_sm_build_expanded_nak(
  1150. struct eap_sm *sm, int id, const struct eap_method *methods,
  1151. size_t count)
  1152. {
  1153. struct wpabuf *resp;
  1154. int found = 0;
  1155. const struct eap_method *m;
  1156. wpa_printf(MSG_DEBUG, "EAP: Building expanded EAP-Nak");
  1157. /* RFC 3748 - 5.3.2: Expanded Nak */
  1158. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_EXPANDED,
  1159. 8 + 8 * (count + 1), EAP_CODE_RESPONSE, id);
  1160. if (resp == NULL)
  1161. return NULL;
  1162. wpabuf_put_be24(resp, EAP_VENDOR_IETF);
  1163. wpabuf_put_be32(resp, EAP_TYPE_NAK);
  1164. for (m = methods; m; m = m->next) {
  1165. if (sm->reqVendor == m->vendor &&
  1166. sm->reqVendorMethod == m->method)
  1167. continue; /* do not allow the current method again */
  1168. if (eap_allowed_method(sm, m->vendor, m->method)) {
  1169. wpa_printf(MSG_DEBUG, "EAP: allowed type: "
  1170. "vendor=%u method=%u",
  1171. m->vendor, m->method);
  1172. wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
  1173. wpabuf_put_be24(resp, m->vendor);
  1174. wpabuf_put_be32(resp, m->method);
  1175. found++;
  1176. }
  1177. }
  1178. if (!found) {
  1179. wpa_printf(MSG_DEBUG, "EAP: no more allowed methods");
  1180. wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
  1181. wpabuf_put_be24(resp, EAP_VENDOR_IETF);
  1182. wpabuf_put_be32(resp, EAP_TYPE_NONE);
  1183. }
  1184. eap_update_len(resp);
  1185. return resp;
  1186. }
  1187. static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id)
  1188. {
  1189. struct wpabuf *resp;
  1190. u8 *start;
  1191. int found = 0, expanded_found = 0;
  1192. size_t count;
  1193. const struct eap_method *methods, *m;
  1194. wpa_printf(MSG_DEBUG, "EAP: Building EAP-Nak (requested type %u "
  1195. "vendor=%u method=%u not allowed)", sm->reqMethod,
  1196. sm->reqVendor, sm->reqVendorMethod);
  1197. methods = eap_peer_get_methods(&count);
  1198. if (methods == NULL)
  1199. return NULL;
  1200. if (sm->reqMethod == EAP_TYPE_EXPANDED)
  1201. return eap_sm_build_expanded_nak(sm, id, methods, count);
  1202. /* RFC 3748 - 5.3.1: Legacy Nak */
  1203. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK,
  1204. sizeof(struct eap_hdr) + 1 + count + 1,
  1205. EAP_CODE_RESPONSE, id);
  1206. if (resp == NULL)
  1207. return NULL;
  1208. start = wpabuf_put(resp, 0);
  1209. for (m = methods; m; m = m->next) {
  1210. if (m->vendor == EAP_VENDOR_IETF && m->method == sm->reqMethod)
  1211. continue; /* do not allow the current method again */
  1212. if (eap_allowed_method(sm, m->vendor, m->method)) {
  1213. if (m->vendor != EAP_VENDOR_IETF) {
  1214. if (expanded_found)
  1215. continue;
  1216. expanded_found = 1;
  1217. wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
  1218. } else
  1219. wpabuf_put_u8(resp, m->method);
  1220. found++;
  1221. }
  1222. }
  1223. if (!found)
  1224. wpabuf_put_u8(resp, EAP_TYPE_NONE);
  1225. wpa_hexdump(MSG_DEBUG, "EAP: allowed methods", start, found);
  1226. eap_update_len(resp);
  1227. return resp;
  1228. }
  1229. static void eap_sm_processIdentity(struct eap_sm *sm, const struct wpabuf *req)
  1230. {
  1231. const u8 *pos;
  1232. size_t msg_len;
  1233. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
  1234. "EAP authentication started");
  1235. eap_notify_status(sm, "started", "");
  1236. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, req,
  1237. &msg_len);
  1238. if (pos == NULL)
  1239. return;
  1240. /*
  1241. * RFC 3748 - 5.1: Identity
  1242. * Data field may contain a displayable message in UTF-8. If this
  1243. * includes NUL-character, only the data before that should be
  1244. * displayed. Some EAP implementasitons may piggy-back additional
  1245. * options after the NUL.
  1246. */
  1247. /* TODO: could save displayable message so that it can be shown to the
  1248. * user in case of interaction is required */
  1249. wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Identity data",
  1250. pos, msg_len);
  1251. }
  1252. #ifdef PCSC_FUNCS
  1253. /*
  1254. * Rules for figuring out MNC length based on IMSI for SIM cards that do not
  1255. * include MNC length field.
  1256. */
  1257. static int mnc_len_from_imsi(const char *imsi)
  1258. {
  1259. char mcc_str[4];
  1260. unsigned int mcc;
  1261. os_memcpy(mcc_str, imsi, 3);
  1262. mcc_str[3] = '\0';
  1263. mcc = atoi(mcc_str);
  1264. if (mcc == 228)
  1265. return 2; /* Networks in Switzerland use 2-digit MNC */
  1266. if (mcc == 244)
  1267. return 2; /* Networks in Finland use 2-digit MNC */
  1268. return -1;
  1269. }
  1270. static int eap_sm_imsi_identity(struct eap_sm *sm,
  1271. struct eap_peer_config *conf)
  1272. {
  1273. enum { EAP_SM_SIM, EAP_SM_AKA, EAP_SM_AKA_PRIME } method = EAP_SM_SIM;
  1274. char imsi[100];
  1275. size_t imsi_len;
  1276. struct eap_method_type *m = conf->eap_methods;
  1277. int i, mnc_len;
  1278. imsi_len = sizeof(imsi);
  1279. if (scard_get_imsi(sm->scard_ctx, imsi, &imsi_len)) {
  1280. wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM");
  1281. return -1;
  1282. }
  1283. wpa_hexdump_ascii(MSG_DEBUG, "IMSI", (u8 *) imsi, imsi_len);
  1284. if (imsi_len < 7) {
  1285. wpa_printf(MSG_WARNING, "Too short IMSI for SIM identity");
  1286. return -1;
  1287. }
  1288. /* MNC (2 or 3 digits) */
  1289. mnc_len = scard_get_mnc_len(sm->scard_ctx);
  1290. if (mnc_len < 0)
  1291. mnc_len = mnc_len_from_imsi(imsi);
  1292. if (mnc_len < 0) {
  1293. wpa_printf(MSG_INFO, "Failed to get MNC length from (U)SIM "
  1294. "assuming 3");
  1295. mnc_len = 3;
  1296. }
  1297. if (eap_sm_append_3gpp_realm(sm, imsi, sizeof(imsi), &imsi_len,
  1298. mnc_len) < 0) {
  1299. wpa_printf(MSG_WARNING, "Could not add realm to SIM identity");
  1300. return -1;
  1301. }
  1302. wpa_hexdump_ascii(MSG_DEBUG, "IMSI + realm", (u8 *) imsi, imsi_len);
  1303. for (i = 0; m && (m[i].vendor != EAP_VENDOR_IETF ||
  1304. m[i].method != EAP_TYPE_NONE); i++) {
  1305. if (m[i].vendor == EAP_VENDOR_IETF &&
  1306. m[i].method == EAP_TYPE_AKA_PRIME) {
  1307. method = EAP_SM_AKA_PRIME;
  1308. break;
  1309. }
  1310. if (m[i].vendor == EAP_VENDOR_IETF &&
  1311. m[i].method == EAP_TYPE_AKA) {
  1312. method = EAP_SM_AKA;
  1313. break;
  1314. }
  1315. }
  1316. os_free(conf->identity);
  1317. conf->identity = os_malloc(1 + imsi_len);
  1318. if (conf->identity == NULL) {
  1319. wpa_printf(MSG_WARNING, "Failed to allocate buffer for "
  1320. "IMSI-based identity");
  1321. return -1;
  1322. }
  1323. switch (method) {
  1324. case EAP_SM_SIM:
  1325. conf->identity[0] = '1';
  1326. break;
  1327. case EAP_SM_AKA:
  1328. conf->identity[0] = '0';
  1329. break;
  1330. case EAP_SM_AKA_PRIME:
  1331. conf->identity[0] = '6';
  1332. break;
  1333. }
  1334. os_memcpy(conf->identity + 1, imsi, imsi_len);
  1335. conf->identity_len = 1 + imsi_len;
  1336. return 0;
  1337. }
  1338. static int eap_sm_set_scard_pin(struct eap_sm *sm,
  1339. struct eap_peer_config *conf)
  1340. {
  1341. if (scard_set_pin(sm->scard_ctx, conf->pin)) {
  1342. /*
  1343. * Make sure the same PIN is not tried again in order to avoid
  1344. * blocking SIM.
  1345. */
  1346. os_free(conf->pin);
  1347. conf->pin = NULL;
  1348. wpa_printf(MSG_WARNING, "PIN validation failed");
  1349. eap_sm_request_pin(sm);
  1350. return -1;
  1351. }
  1352. return 0;
  1353. }
  1354. static int eap_sm_get_scard_identity(struct eap_sm *sm,
  1355. struct eap_peer_config *conf)
  1356. {
  1357. if (eap_sm_set_scard_pin(sm, conf))
  1358. return -1;
  1359. return eap_sm_imsi_identity(sm, conf);
  1360. }
  1361. #endif /* PCSC_FUNCS */
  1362. /**
  1363. * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
  1364. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  1365. * @id: EAP identifier for the packet
  1366. * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
  1367. * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
  1368. * failure
  1369. *
  1370. * This function allocates and builds an EAP-Identity/Response packet for the
  1371. * current network. The caller is responsible for freeing the returned data.
  1372. */
  1373. struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted)
  1374. {
  1375. struct eap_peer_config *config = eap_get_config(sm);
  1376. struct wpabuf *resp;
  1377. const u8 *identity;
  1378. size_t identity_len;
  1379. if (config == NULL) {
  1380. wpa_printf(MSG_WARNING, "EAP: buildIdentity: configuration "
  1381. "was not available");
  1382. return NULL;
  1383. }
  1384. if (sm->m && sm->m->get_identity &&
  1385. (identity = sm->m->get_identity(sm, sm->eap_method_priv,
  1386. &identity_len)) != NULL) {
  1387. wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth "
  1388. "identity", identity, identity_len);
  1389. } else if (!encrypted && config->anonymous_identity) {
  1390. identity = config->anonymous_identity;
  1391. identity_len = config->anonymous_identity_len;
  1392. wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity",
  1393. identity, identity_len);
  1394. } else {
  1395. identity = config->identity;
  1396. identity_len = config->identity_len;
  1397. wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity",
  1398. identity, identity_len);
  1399. }
  1400. if (config->pcsc) {
  1401. #ifdef PCSC_FUNCS
  1402. if (!identity) {
  1403. if (eap_sm_get_scard_identity(sm, config) < 0)
  1404. return NULL;
  1405. identity = config->identity;
  1406. identity_len = config->identity_len;
  1407. wpa_hexdump_ascii(MSG_DEBUG,
  1408. "permanent identity from IMSI",
  1409. identity, identity_len);
  1410. } else if (eap_sm_set_scard_pin(sm, config) < 0) {
  1411. return NULL;
  1412. }
  1413. #else /* PCSC_FUNCS */
  1414. return NULL;
  1415. #endif /* PCSC_FUNCS */
  1416. } else if (!identity) {
  1417. wpa_printf(MSG_WARNING,
  1418. "EAP: buildIdentity: identity configuration was not available");
  1419. eap_sm_request_identity(sm);
  1420. return NULL;
  1421. }
  1422. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, identity_len,
  1423. EAP_CODE_RESPONSE, id);
  1424. if (resp == NULL)
  1425. return NULL;
  1426. wpabuf_put_data(resp, identity, identity_len);
  1427. return resp;
  1428. }
  1429. static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req)
  1430. {
  1431. const u8 *pos;
  1432. char *msg;
  1433. size_t i, msg_len;
  1434. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, req,
  1435. &msg_len);
  1436. if (pos == NULL)
  1437. return;
  1438. wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Notification data",
  1439. pos, msg_len);
  1440. msg = os_malloc(msg_len + 1);
  1441. if (msg == NULL)
  1442. return;
  1443. for (i = 0; i < msg_len; i++)
  1444. msg[i] = isprint(pos[i]) ? (char) pos[i] : '_';
  1445. msg[msg_len] = '\0';
  1446. wpa_msg(sm->msg_ctx, MSG_INFO, "%s%s",
  1447. WPA_EVENT_EAP_NOTIFICATION, msg);
  1448. os_free(msg);
  1449. }
  1450. static struct wpabuf * eap_sm_buildNotify(int id)
  1451. {
  1452. wpa_printf(MSG_DEBUG, "EAP: Generating EAP-Response Notification");
  1453. return eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, 0,
  1454. EAP_CODE_RESPONSE, id);
  1455. }
  1456. static void eap_peer_initiate(struct eap_sm *sm, const struct eap_hdr *hdr,
  1457. size_t len)
  1458. {
  1459. #ifdef CONFIG_ERP
  1460. const u8 *pos = (const u8 *) (hdr + 1);
  1461. const u8 *end = ((const u8 *) hdr) + len;
  1462. struct erp_tlvs parse;
  1463. if (len < sizeof(*hdr) + 1) {
  1464. wpa_printf(MSG_DEBUG, "EAP: Ignored too short EAP-Initiate");
  1465. return;
  1466. }
  1467. if (*pos != EAP_ERP_TYPE_REAUTH_START) {
  1468. wpa_printf(MSG_DEBUG,
  1469. "EAP: Ignored unexpected EAP-Initiate Type=%u",
  1470. *pos);
  1471. return;
  1472. }
  1473. pos++;
  1474. if (pos >= end) {
  1475. wpa_printf(MSG_DEBUG,
  1476. "EAP: Too short EAP-Initiate/Re-auth-Start");
  1477. return;
  1478. }
  1479. pos++; /* Reserved */
  1480. wpa_hexdump(MSG_DEBUG, "EAP: EAP-Initiate/Re-auth-Start TVs/TLVs",
  1481. pos, end - pos);
  1482. if (erp_parse_tlvs(pos, end, &parse, 0) < 0)
  1483. goto invalid;
  1484. if (parse.domain) {
  1485. wpa_hexdump_ascii(MSG_DEBUG,
  1486. "EAP: EAP-Initiate/Re-auth-Start - Domain name",
  1487. parse.domain, parse.domain_len);
  1488. /* TODO: Derivation of domain specific keys for local ER */
  1489. }
  1490. if (eap_peer_erp_reauth_start(sm, hdr->identifier) == 0)
  1491. return;
  1492. invalid:
  1493. #endif /* CONFIG_ERP */
  1494. wpa_printf(MSG_DEBUG,
  1495. "EAP: EAP-Initiate/Re-auth-Start - No suitable ERP keys available - try to start full EAP authentication");
  1496. eapol_set_bool(sm, EAPOL_eapTriggerStart, TRUE);
  1497. }
  1498. void eap_peer_finish(struct eap_sm *sm, const struct eap_hdr *hdr, size_t len)
  1499. {
  1500. #ifdef CONFIG_ERP
  1501. const u8 *pos = (const u8 *) (hdr + 1);
  1502. const u8 *end = ((const u8 *) hdr) + len;
  1503. const u8 *start;
  1504. struct erp_tlvs parse;
  1505. u8 flags;
  1506. u16 seq;
  1507. u8 hash[SHA256_MAC_LEN];
  1508. size_t hash_len;
  1509. struct eap_erp_key *erp;
  1510. int max_len;
  1511. char nai[254];
  1512. u8 seed[4];
  1513. int auth_tag_ok = 0;
  1514. if (len < sizeof(*hdr) + 1) {
  1515. wpa_printf(MSG_DEBUG, "EAP: Ignored too short EAP-Finish");
  1516. return;
  1517. }
  1518. if (*pos != EAP_ERP_TYPE_REAUTH) {
  1519. wpa_printf(MSG_DEBUG,
  1520. "EAP: Ignored unexpected EAP-Finish Type=%u", *pos);
  1521. return;
  1522. }
  1523. if (len < sizeof(*hdr) + 4) {
  1524. wpa_printf(MSG_DEBUG,
  1525. "EAP: Ignored too short EAP-Finish/Re-auth");
  1526. return;
  1527. }
  1528. pos++;
  1529. flags = *pos++;
  1530. seq = WPA_GET_BE16(pos);
  1531. pos += 2;
  1532. wpa_printf(MSG_DEBUG, "EAP: Flags=0x%x SEQ=%u", flags, seq);
  1533. if (seq != sm->erp_seq) {
  1534. wpa_printf(MSG_DEBUG,
  1535. "EAP: Unexpected EAP-Finish/Re-auth SEQ=%u", seq);
  1536. return;
  1537. }
  1538. /*
  1539. * Parse TVs/TLVs. Since we do not yet know the length of the
  1540. * Authentication Tag, stop parsing if an unknown TV/TLV is seen and
  1541. * just try to find the keyName-NAI first so that we can check the
  1542. * Authentication Tag.
  1543. */
  1544. if (erp_parse_tlvs(pos, end, &parse, 1) < 0)
  1545. return;
  1546. if (!parse.keyname) {
  1547. wpa_printf(MSG_DEBUG,
  1548. "EAP: No keyName-NAI in EAP-Finish/Re-auth Packet");
  1549. return;
  1550. }
  1551. wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Finish/Re-auth - keyName-NAI",
  1552. parse.keyname, parse.keyname_len);
  1553. if (parse.keyname_len > 253) {
  1554. wpa_printf(MSG_DEBUG,
  1555. "EAP: Too long keyName-NAI in EAP-Finish/Re-auth");
  1556. return;
  1557. }
  1558. os_memcpy(nai, parse.keyname, parse.keyname_len);
  1559. nai[parse.keyname_len] = '\0';
  1560. erp = eap_erp_get_key_nai(sm, nai);
  1561. if (!erp) {
  1562. wpa_printf(MSG_DEBUG, "EAP: No matching ERP key found for %s",
  1563. nai);
  1564. return;
  1565. }
  1566. /* Is there enough room for Cryptosuite and Authentication Tag? */
  1567. start = parse.keyname + parse.keyname_len;
  1568. max_len = end - start;
  1569. hash_len = 16;
  1570. if (max_len < 1 + (int) hash_len) {
  1571. wpa_printf(MSG_DEBUG,
  1572. "EAP: Not enough room for Authentication Tag");
  1573. if (flags & 0x80)
  1574. goto no_auth_tag;
  1575. return;
  1576. }
  1577. if (end[-17] != EAP_ERP_CS_HMAC_SHA256_128) {
  1578. wpa_printf(MSG_DEBUG, "EAP: Different Cryptosuite used");
  1579. if (flags & 0x80)
  1580. goto no_auth_tag;
  1581. return;
  1582. }
  1583. if (hmac_sha256(erp->rIK, erp->rIK_len, (const u8 *) hdr,
  1584. end - ((const u8 *) hdr) - hash_len, hash) < 0)
  1585. return;
  1586. if (os_memcmp(end - hash_len, hash, hash_len) != 0) {
  1587. wpa_printf(MSG_DEBUG,
  1588. "EAP: Authentication Tag mismatch");
  1589. return;
  1590. }
  1591. auth_tag_ok = 1;
  1592. end -= 1 + hash_len;
  1593. no_auth_tag:
  1594. /*
  1595. * Parse TVs/TLVs again now that we know the exact part of the buffer
  1596. * that contains them.
  1597. */
  1598. wpa_hexdump(MSG_DEBUG, "EAP: EAP-Finish/Re-Auth TVs/TLVs",
  1599. pos, end - pos);
  1600. if (erp_parse_tlvs(pos, end, &parse, 0) < 0)
  1601. return;
  1602. if (flags & 0x80 || !auth_tag_ok) {
  1603. wpa_printf(MSG_DEBUG,
  1604. "EAP: EAP-Finish/Re-auth indicated failure");
  1605. eapol_set_bool(sm, EAPOL_eapFail, TRUE);
  1606. eapol_set_bool(sm, EAPOL_eapReq, FALSE);
  1607. eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
  1608. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
  1609. "EAP authentication failed");
  1610. sm->prev_failure = 1;
  1611. wpa_printf(MSG_DEBUG,
  1612. "EAP: Drop ERP key to try full authentication on next attempt");
  1613. eap_peer_erp_free_key(erp);
  1614. return;
  1615. }
  1616. eap_sm_free_key(sm);
  1617. sm->eapKeyDataLen = 0;
  1618. sm->eapKeyData = os_malloc(erp->rRK_len);
  1619. if (!sm->eapKeyData)
  1620. return;
  1621. sm->eapKeyDataLen = erp->rRK_len;
  1622. WPA_PUT_BE16(seed, seq);
  1623. WPA_PUT_BE16(&seed[2], erp->rRK_len);
  1624. if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
  1625. "Re-authentication Master Session Key@ietf.org",
  1626. seed, sizeof(seed),
  1627. sm->eapKeyData, erp->rRK_len) < 0) {
  1628. wpa_printf(MSG_DEBUG, "EAP: Could not derive rMSK for ERP");
  1629. eap_sm_free_key(sm);
  1630. return;
  1631. }
  1632. wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rMSK",
  1633. sm->eapKeyData, sm->eapKeyDataLen);
  1634. sm->eapKeyAvailable = TRUE;
  1635. eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
  1636. eapol_set_bool(sm, EAPOL_eapReq, FALSE);
  1637. eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
  1638. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
  1639. "EAP re-authentication completed successfully");
  1640. #endif /* CONFIG_ERP */
  1641. }
  1642. static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req)
  1643. {
  1644. const struct eap_hdr *hdr;
  1645. size_t plen;
  1646. const u8 *pos;
  1647. sm->rxReq = sm->rxResp = sm->rxSuccess = sm->rxFailure = FALSE;
  1648. sm->reqId = 0;
  1649. sm->reqMethod = EAP_TYPE_NONE;
  1650. sm->reqVendor = EAP_VENDOR_IETF;
  1651. sm->reqVendorMethod = EAP_TYPE_NONE;
  1652. if (req == NULL || wpabuf_len(req) < sizeof(*hdr))
  1653. return;
  1654. hdr = wpabuf_head(req);
  1655. plen = be_to_host16(hdr->length);
  1656. if (plen > wpabuf_len(req)) {
  1657. wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
  1658. "(len=%lu plen=%lu)",
  1659. (unsigned long) wpabuf_len(req),
  1660. (unsigned long) plen);
  1661. return;
  1662. }
  1663. sm->reqId = hdr->identifier;
  1664. if (sm->workaround) {
  1665. const u8 *addr[1];
  1666. addr[0] = wpabuf_head(req);
  1667. sha1_vector(1, addr, &plen, sm->req_sha1);
  1668. }
  1669. switch (hdr->code) {
  1670. case EAP_CODE_REQUEST:
  1671. if (plen < sizeof(*hdr) + 1) {
  1672. wpa_printf(MSG_DEBUG, "EAP: Too short EAP-Request - "
  1673. "no Type field");
  1674. return;
  1675. }
  1676. sm->rxReq = TRUE;
  1677. pos = (const u8 *) (hdr + 1);
  1678. sm->reqMethod = *pos++;
  1679. if (sm->reqMethod == EAP_TYPE_EXPANDED) {
  1680. if (plen < sizeof(*hdr) + 8) {
  1681. wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
  1682. "expanded EAP-Packet (plen=%lu)",
  1683. (unsigned long) plen);
  1684. return;
  1685. }
  1686. sm->reqVendor = WPA_GET_BE24(pos);
  1687. pos += 3;
  1688. sm->reqVendorMethod = WPA_GET_BE32(pos);
  1689. }
  1690. wpa_printf(MSG_DEBUG, "EAP: Received EAP-Request id=%d "
  1691. "method=%u vendor=%u vendorMethod=%u",
  1692. sm->reqId, sm->reqMethod, sm->reqVendor,
  1693. sm->reqVendorMethod);
  1694. break;
  1695. case EAP_CODE_RESPONSE:
  1696. if (sm->selectedMethod == EAP_TYPE_LEAP) {
  1697. /*
  1698. * LEAP differs from RFC 4137 by using reversed roles
  1699. * for mutual authentication and because of this, we
  1700. * need to accept EAP-Response frames if LEAP is used.
  1701. */
  1702. if (plen < sizeof(*hdr) + 1) {
  1703. wpa_printf(MSG_DEBUG, "EAP: Too short "
  1704. "EAP-Response - no Type field");
  1705. return;
  1706. }
  1707. sm->rxResp = TRUE;
  1708. pos = (const u8 *) (hdr + 1);
  1709. sm->reqMethod = *pos;
  1710. wpa_printf(MSG_DEBUG, "EAP: Received EAP-Response for "
  1711. "LEAP method=%d id=%d",
  1712. sm->reqMethod, sm->reqId);
  1713. break;
  1714. }
  1715. wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Response");
  1716. break;
  1717. case EAP_CODE_SUCCESS:
  1718. wpa_printf(MSG_DEBUG, "EAP: Received EAP-Success");
  1719. eap_notify_status(sm, "completion", "success");
  1720. sm->rxSuccess = TRUE;
  1721. break;
  1722. case EAP_CODE_FAILURE:
  1723. wpa_printf(MSG_DEBUG, "EAP: Received EAP-Failure");
  1724. eap_notify_status(sm, "completion", "failure");
  1725. sm->rxFailure = TRUE;
  1726. break;
  1727. case EAP_CODE_INITIATE:
  1728. eap_peer_initiate(sm, hdr, plen);
  1729. break;
  1730. case EAP_CODE_FINISH:
  1731. eap_peer_finish(sm, hdr, plen);
  1732. break;
  1733. default:
  1734. wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Packet with unknown "
  1735. "code %d", hdr->code);
  1736. break;
  1737. }
  1738. }
  1739. static void eap_peer_sm_tls_event(void *ctx, enum tls_event ev,
  1740. union tls_event_data *data)
  1741. {
  1742. struct eap_sm *sm = ctx;
  1743. char *hash_hex = NULL;
  1744. switch (ev) {
  1745. case TLS_CERT_CHAIN_SUCCESS:
  1746. eap_notify_status(sm, "remote certificate verification",
  1747. "success");
  1748. if (sm->ext_cert_check) {
  1749. sm->waiting_ext_cert_check = 1;
  1750. eap_sm_request(sm, WPA_CTRL_REQ_EXT_CERT_CHECK,
  1751. NULL, 0);
  1752. }
  1753. break;
  1754. case TLS_CERT_CHAIN_FAILURE:
  1755. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_TLS_CERT_ERROR
  1756. "reason=%d depth=%d subject='%s' err='%s'",
  1757. data->cert_fail.reason,
  1758. data->cert_fail.depth,
  1759. data->cert_fail.subject,
  1760. data->cert_fail.reason_txt);
  1761. eap_notify_status(sm, "remote certificate verification",
  1762. data->cert_fail.reason_txt);
  1763. break;
  1764. case TLS_PEER_CERTIFICATE:
  1765. if (!sm->eapol_cb->notify_cert)
  1766. break;
  1767. if (data->peer_cert.hash) {
  1768. size_t len = data->peer_cert.hash_len * 2 + 1;
  1769. hash_hex = os_malloc(len);
  1770. if (hash_hex) {
  1771. wpa_snprintf_hex(hash_hex, len,
  1772. data->peer_cert.hash,
  1773. data->peer_cert.hash_len);
  1774. }
  1775. }
  1776. sm->eapol_cb->notify_cert(sm->eapol_ctx,
  1777. data->peer_cert.depth,
  1778. data->peer_cert.subject,
  1779. data->peer_cert.altsubject,
  1780. data->peer_cert.num_altsubject,
  1781. hash_hex, data->peer_cert.cert);
  1782. break;
  1783. case TLS_ALERT:
  1784. if (data->alert.is_local)
  1785. eap_notify_status(sm, "local TLS alert",
  1786. data->alert.description);
  1787. else
  1788. eap_notify_status(sm, "remote TLS alert",
  1789. data->alert.description);
  1790. break;
  1791. }
  1792. os_free(hash_hex);
  1793. }
  1794. /**
  1795. * eap_peer_sm_init - Allocate and initialize EAP peer state machine
  1796. * @eapol_ctx: Context data to be used with eapol_cb calls
  1797. * @eapol_cb: Pointer to EAPOL callback functions
  1798. * @msg_ctx: Context data for wpa_msg() calls
  1799. * @conf: EAP configuration
  1800. * Returns: Pointer to the allocated EAP state machine or %NULL on failure
  1801. *
  1802. * This function allocates and initializes an EAP state machine. In addition,
  1803. * this initializes TLS library for the new EAP state machine. eapol_cb pointer
  1804. * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
  1805. * state machine. Consequently, the caller must make sure that this data
  1806. * structure remains alive while the EAP state machine is active.
  1807. */
  1808. struct eap_sm * eap_peer_sm_init(void *eapol_ctx,
  1809. const struct eapol_callbacks *eapol_cb,
  1810. void *msg_ctx, struct eap_config *conf)
  1811. {
  1812. struct eap_sm *sm;
  1813. struct tls_config tlsconf;
  1814. sm = os_zalloc(sizeof(*sm));
  1815. if (sm == NULL)
  1816. return NULL;
  1817. sm->eapol_ctx = eapol_ctx;
  1818. sm->eapol_cb = eapol_cb;
  1819. sm->msg_ctx = msg_ctx;
  1820. sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
  1821. sm->wps = conf->wps;
  1822. dl_list_init(&sm->erp_keys);
  1823. os_memset(&tlsconf, 0, sizeof(tlsconf));
  1824. tlsconf.opensc_engine_path = conf->opensc_engine_path;
  1825. tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path;
  1826. tlsconf.pkcs11_module_path = conf->pkcs11_module_path;
  1827. tlsconf.openssl_ciphers = conf->openssl_ciphers;
  1828. #ifdef CONFIG_FIPS
  1829. tlsconf.fips_mode = 1;
  1830. #endif /* CONFIG_FIPS */
  1831. tlsconf.event_cb = eap_peer_sm_tls_event;
  1832. tlsconf.cb_ctx = sm;
  1833. tlsconf.cert_in_cb = conf->cert_in_cb;
  1834. sm->ssl_ctx = tls_init(&tlsconf);
  1835. if (sm->ssl_ctx == NULL) {
  1836. wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS "
  1837. "context.");
  1838. os_free(sm);
  1839. return NULL;
  1840. }
  1841. sm->ssl_ctx2 = tls_init(&tlsconf);
  1842. if (sm->ssl_ctx2 == NULL) {
  1843. wpa_printf(MSG_INFO, "SSL: Failed to initialize TLS "
  1844. "context (2).");
  1845. /* Run without separate TLS context within TLS tunnel */
  1846. }
  1847. return sm;
  1848. }
  1849. /**
  1850. * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
  1851. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  1852. *
  1853. * This function deinitializes EAP state machine and frees all allocated
  1854. * resources.
  1855. */
  1856. void eap_peer_sm_deinit(struct eap_sm *sm)
  1857. {
  1858. if (sm == NULL)
  1859. return;
  1860. eap_deinit_prev_method(sm, "EAP deinit");
  1861. eap_sm_abort(sm);
  1862. if (sm->ssl_ctx2)
  1863. tls_deinit(sm->ssl_ctx2);
  1864. tls_deinit(sm->ssl_ctx);
  1865. eap_peer_erp_free_keys(sm);
  1866. os_free(sm);
  1867. }
  1868. /**
  1869. * eap_peer_sm_step - Step EAP peer state machine
  1870. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  1871. * Returns: 1 if EAP state was changed or 0 if not
  1872. *
  1873. * This function advances EAP state machine to a new state to match with the
  1874. * current variables. This should be called whenever variables used by the EAP
  1875. * state machine have changed.
  1876. */
  1877. int eap_peer_sm_step(struct eap_sm *sm)
  1878. {
  1879. int res = 0;
  1880. do {
  1881. sm->changed = FALSE;
  1882. SM_STEP_RUN(EAP);
  1883. if (sm->changed)
  1884. res = 1;
  1885. } while (sm->changed);
  1886. return res;
  1887. }
  1888. /**
  1889. * eap_sm_abort - Abort EAP authentication
  1890. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  1891. *
  1892. * Release system resources that have been allocated for the authentication
  1893. * session without fully deinitializing the EAP state machine.
  1894. */
  1895. void eap_sm_abort(struct eap_sm *sm)
  1896. {
  1897. wpabuf_free(sm->lastRespData);
  1898. sm->lastRespData = NULL;
  1899. wpabuf_free(sm->eapRespData);
  1900. sm->eapRespData = NULL;
  1901. eap_sm_free_key(sm);
  1902. os_free(sm->eapSessionId);
  1903. sm->eapSessionId = NULL;
  1904. /* This is not clearly specified in the EAP statemachines draft, but
  1905. * it seems necessary to make sure that some of the EAPOL variables get
  1906. * cleared for the next authentication. */
  1907. eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
  1908. }
  1909. #ifdef CONFIG_CTRL_IFACE
  1910. static const char * eap_sm_state_txt(int state)
  1911. {
  1912. switch (state) {
  1913. case EAP_INITIALIZE:
  1914. return "INITIALIZE";
  1915. case EAP_DISABLED:
  1916. return "DISABLED";
  1917. case EAP_IDLE:
  1918. return "IDLE";
  1919. case EAP_RECEIVED:
  1920. return "RECEIVED";
  1921. case EAP_GET_METHOD:
  1922. return "GET_METHOD";
  1923. case EAP_METHOD:
  1924. return "METHOD";
  1925. case EAP_SEND_RESPONSE:
  1926. return "SEND_RESPONSE";
  1927. case EAP_DISCARD:
  1928. return "DISCARD";
  1929. case EAP_IDENTITY:
  1930. return "IDENTITY";
  1931. case EAP_NOTIFICATION:
  1932. return "NOTIFICATION";
  1933. case EAP_RETRANSMIT:
  1934. return "RETRANSMIT";
  1935. case EAP_SUCCESS:
  1936. return "SUCCESS";
  1937. case EAP_FAILURE:
  1938. return "FAILURE";
  1939. default:
  1940. return "UNKNOWN";
  1941. }
  1942. }
  1943. #endif /* CONFIG_CTRL_IFACE */
  1944. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  1945. static const char * eap_sm_method_state_txt(EapMethodState state)
  1946. {
  1947. switch (state) {
  1948. case METHOD_NONE:
  1949. return "NONE";
  1950. case METHOD_INIT:
  1951. return "INIT";
  1952. case METHOD_CONT:
  1953. return "CONT";
  1954. case METHOD_MAY_CONT:
  1955. return "MAY_CONT";
  1956. case METHOD_DONE:
  1957. return "DONE";
  1958. default:
  1959. return "UNKNOWN";
  1960. }
  1961. }
  1962. static const char * eap_sm_decision_txt(EapDecision decision)
  1963. {
  1964. switch (decision) {
  1965. case DECISION_FAIL:
  1966. return "FAIL";
  1967. case DECISION_COND_SUCC:
  1968. return "COND_SUCC";
  1969. case DECISION_UNCOND_SUCC:
  1970. return "UNCOND_SUCC";
  1971. default:
  1972. return "UNKNOWN";
  1973. }
  1974. }
  1975. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  1976. #ifdef CONFIG_CTRL_IFACE
  1977. /**
  1978. * eap_sm_get_status - Get EAP state machine status
  1979. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  1980. * @buf: Buffer for status information
  1981. * @buflen: Maximum buffer length
  1982. * @verbose: Whether to include verbose status information
  1983. * Returns: Number of bytes written to buf.
  1984. *
  1985. * Query EAP state machine for status information. This function fills in a
  1986. * text area with current status information from the EAPOL state machine. If
  1987. * the buffer (buf) is not large enough, status information will be truncated
  1988. * to fit the buffer.
  1989. */
  1990. int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose)
  1991. {
  1992. int len, ret;
  1993. if (sm == NULL)
  1994. return 0;
  1995. len = os_snprintf(buf, buflen,
  1996. "EAP state=%s\n",
  1997. eap_sm_state_txt(sm->EAP_state));
  1998. if (os_snprintf_error(buflen, len))
  1999. return 0;
  2000. if (sm->selectedMethod != EAP_TYPE_NONE) {
  2001. const char *name;
  2002. if (sm->m) {
  2003. name = sm->m->name;
  2004. } else {
  2005. const struct eap_method *m =
  2006. eap_peer_get_eap_method(EAP_VENDOR_IETF,
  2007. sm->selectedMethod);
  2008. if (m)
  2009. name = m->name;
  2010. else
  2011. name = "?";
  2012. }
  2013. ret = os_snprintf(buf + len, buflen - len,
  2014. "selectedMethod=%d (EAP-%s)\n",
  2015. sm->selectedMethod, name);
  2016. if (os_snprintf_error(buflen - len, ret))
  2017. return len;
  2018. len += ret;
  2019. if (sm->m && sm->m->get_status) {
  2020. len += sm->m->get_status(sm, sm->eap_method_priv,
  2021. buf + len, buflen - len,
  2022. verbose);
  2023. }
  2024. }
  2025. if (verbose) {
  2026. ret = os_snprintf(buf + len, buflen - len,
  2027. "reqMethod=%d\n"
  2028. "methodState=%s\n"
  2029. "decision=%s\n"
  2030. "ClientTimeout=%d\n",
  2031. sm->reqMethod,
  2032. eap_sm_method_state_txt(sm->methodState),
  2033. eap_sm_decision_txt(sm->decision),
  2034. sm->ClientTimeout);
  2035. if (os_snprintf_error(buflen - len, ret))
  2036. return len;
  2037. len += ret;
  2038. }
  2039. return len;
  2040. }
  2041. #endif /* CONFIG_CTRL_IFACE */
  2042. static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field,
  2043. const char *msg, size_t msglen)
  2044. {
  2045. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  2046. struct eap_peer_config *config;
  2047. const char *txt = NULL;
  2048. char *tmp;
  2049. if (sm == NULL)
  2050. return;
  2051. config = eap_get_config(sm);
  2052. if (config == NULL)
  2053. return;
  2054. switch (field) {
  2055. case WPA_CTRL_REQ_EAP_IDENTITY:
  2056. config->pending_req_identity++;
  2057. break;
  2058. case WPA_CTRL_REQ_EAP_PASSWORD:
  2059. config->pending_req_password++;
  2060. break;
  2061. case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
  2062. config->pending_req_new_password++;
  2063. break;
  2064. case WPA_CTRL_REQ_EAP_PIN:
  2065. config->pending_req_pin++;
  2066. break;
  2067. case WPA_CTRL_REQ_EAP_OTP:
  2068. if (msg) {
  2069. tmp = os_malloc(msglen + 3);
  2070. if (tmp == NULL)
  2071. return;
  2072. tmp[0] = '[';
  2073. os_memcpy(tmp + 1, msg, msglen);
  2074. tmp[msglen + 1] = ']';
  2075. tmp[msglen + 2] = '\0';
  2076. txt = tmp;
  2077. os_free(config->pending_req_otp);
  2078. config->pending_req_otp = tmp;
  2079. config->pending_req_otp_len = msglen + 3;
  2080. } else {
  2081. if (config->pending_req_otp == NULL)
  2082. return;
  2083. txt = config->pending_req_otp;
  2084. }
  2085. break;
  2086. case WPA_CTRL_REQ_EAP_PASSPHRASE:
  2087. config->pending_req_passphrase++;
  2088. break;
  2089. case WPA_CTRL_REQ_SIM:
  2090. config->pending_req_sim++;
  2091. txt = msg;
  2092. break;
  2093. case WPA_CTRL_REQ_EXT_CERT_CHECK:
  2094. break;
  2095. default:
  2096. return;
  2097. }
  2098. if (sm->eapol_cb->eap_param_needed)
  2099. sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt);
  2100. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  2101. }
  2102. const char * eap_sm_get_method_name(struct eap_sm *sm)
  2103. {
  2104. if (sm->m == NULL)
  2105. return "UNKNOWN";
  2106. return sm->m->name;
  2107. }
  2108. /**
  2109. * eap_sm_request_identity - Request identity from user (ctrl_iface)
  2110. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2111. *
  2112. * EAP methods can call this function to request identity information for the
  2113. * current network. This is normally called when the identity is not included
  2114. * in the network configuration. The request will be sent to monitor programs
  2115. * through the control interface.
  2116. */
  2117. void eap_sm_request_identity(struct eap_sm *sm)
  2118. {
  2119. eap_sm_request(sm, WPA_CTRL_REQ_EAP_IDENTITY, NULL, 0);
  2120. }
  2121. /**
  2122. * eap_sm_request_password - Request password from user (ctrl_iface)
  2123. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2124. *
  2125. * EAP methods can call this function to request password information for the
  2126. * current network. This is normally called when the password is not included
  2127. * in the network configuration. The request will be sent to monitor programs
  2128. * through the control interface.
  2129. */
  2130. void eap_sm_request_password(struct eap_sm *sm)
  2131. {
  2132. eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSWORD, NULL, 0);
  2133. }
  2134. /**
  2135. * eap_sm_request_new_password - Request new password from user (ctrl_iface)
  2136. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2137. *
  2138. * EAP methods can call this function to request new password information for
  2139. * the current network. This is normally called when the EAP method indicates
  2140. * that the current password has expired and password change is required. The
  2141. * request will be sent to monitor programs through the control interface.
  2142. */
  2143. void eap_sm_request_new_password(struct eap_sm *sm)
  2144. {
  2145. eap_sm_request(sm, WPA_CTRL_REQ_EAP_NEW_PASSWORD, NULL, 0);
  2146. }
  2147. /**
  2148. * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
  2149. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2150. *
  2151. * EAP methods can call this function to request SIM or smart card PIN
  2152. * information for the current network. This is normally called when the PIN is
  2153. * not included in the network configuration. The request will be sent to
  2154. * monitor programs through the control interface.
  2155. */
  2156. void eap_sm_request_pin(struct eap_sm *sm)
  2157. {
  2158. eap_sm_request(sm, WPA_CTRL_REQ_EAP_PIN, NULL, 0);
  2159. }
  2160. /**
  2161. * eap_sm_request_otp - Request one time password from user (ctrl_iface)
  2162. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2163. * @msg: Message to be displayed to the user when asking for OTP
  2164. * @msg_len: Length of the user displayable message
  2165. *
  2166. * EAP methods can call this function to request open time password (OTP) for
  2167. * the current network. The request will be sent to monitor programs through
  2168. * the control interface.
  2169. */
  2170. void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len)
  2171. {
  2172. eap_sm_request(sm, WPA_CTRL_REQ_EAP_OTP, msg, msg_len);
  2173. }
  2174. /**
  2175. * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
  2176. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2177. *
  2178. * EAP methods can call this function to request passphrase for a private key
  2179. * for the current network. This is normally called when the passphrase is not
  2180. * included in the network configuration. The request will be sent to monitor
  2181. * programs through the control interface.
  2182. */
  2183. void eap_sm_request_passphrase(struct eap_sm *sm)
  2184. {
  2185. eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSPHRASE, NULL, 0);
  2186. }
  2187. /**
  2188. * eap_sm_request_sim - Request external SIM processing
  2189. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2190. * @req: EAP method specific request
  2191. */
  2192. void eap_sm_request_sim(struct eap_sm *sm, const char *req)
  2193. {
  2194. eap_sm_request(sm, WPA_CTRL_REQ_SIM, req, os_strlen(req));
  2195. }
  2196. /**
  2197. * eap_sm_notify_ctrl_attached - Notification of attached monitor
  2198. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2199. *
  2200. * Notify EAP state machines that a monitor was attached to the control
  2201. * interface to trigger re-sending of pending requests for user input.
  2202. */
  2203. void eap_sm_notify_ctrl_attached(struct eap_sm *sm)
  2204. {
  2205. struct eap_peer_config *config = eap_get_config(sm);
  2206. if (config == NULL)
  2207. return;
  2208. /* Re-send any pending requests for user data since a new control
  2209. * interface was added. This handles cases where the EAP authentication
  2210. * starts immediately after system startup when the user interface is
  2211. * not yet running. */
  2212. if (config->pending_req_identity)
  2213. eap_sm_request_identity(sm);
  2214. if (config->pending_req_password)
  2215. eap_sm_request_password(sm);
  2216. if (config->pending_req_new_password)
  2217. eap_sm_request_new_password(sm);
  2218. if (config->pending_req_otp)
  2219. eap_sm_request_otp(sm, NULL, 0);
  2220. if (config->pending_req_pin)
  2221. eap_sm_request_pin(sm);
  2222. if (config->pending_req_passphrase)
  2223. eap_sm_request_passphrase(sm);
  2224. }
  2225. static int eap_allowed_phase2_type(int vendor, int type)
  2226. {
  2227. if (vendor != EAP_VENDOR_IETF)
  2228. return 0;
  2229. return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS &&
  2230. type != EAP_TYPE_FAST;
  2231. }
  2232. /**
  2233. * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
  2234. * @name: EAP method name, e.g., MD5
  2235. * @vendor: Buffer for returning EAP Vendor-Id
  2236. * Returns: EAP method type or %EAP_TYPE_NONE if not found
  2237. *
  2238. * This function maps EAP type names into EAP type numbers that are allowed for
  2239. * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
  2240. * EAP-PEAP, EAP-TTLS, and EAP-FAST.
  2241. */
  2242. u32 eap_get_phase2_type(const char *name, int *vendor)
  2243. {
  2244. int v;
  2245. u32 type = eap_peer_get_type(name, &v);
  2246. if (eap_allowed_phase2_type(v, type)) {
  2247. *vendor = v;
  2248. return type;
  2249. }
  2250. *vendor = EAP_VENDOR_IETF;
  2251. return EAP_TYPE_NONE;
  2252. }
  2253. /**
  2254. * eap_get_phase2_types - Get list of allowed EAP phase 2 types
  2255. * @config: Pointer to a network configuration
  2256. * @count: Pointer to a variable to be filled with number of returned EAP types
  2257. * Returns: Pointer to allocated type list or %NULL on failure
  2258. *
  2259. * This function generates an array of allowed EAP phase 2 (tunneled) types for
  2260. * the given network configuration.
  2261. */
  2262. struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config,
  2263. size_t *count)
  2264. {
  2265. struct eap_method_type *buf;
  2266. u32 method;
  2267. int vendor;
  2268. size_t mcount;
  2269. const struct eap_method *methods, *m;
  2270. methods = eap_peer_get_methods(&mcount);
  2271. if (methods == NULL)
  2272. return NULL;
  2273. *count = 0;
  2274. buf = os_malloc(mcount * sizeof(struct eap_method_type));
  2275. if (buf == NULL)
  2276. return NULL;
  2277. for (m = methods; m; m = m->next) {
  2278. vendor = m->vendor;
  2279. method = m->method;
  2280. if (eap_allowed_phase2_type(vendor, method)) {
  2281. if (vendor == EAP_VENDOR_IETF &&
  2282. method == EAP_TYPE_TLS && config &&
  2283. config->private_key2 == NULL)
  2284. continue;
  2285. buf[*count].vendor = vendor;
  2286. buf[*count].method = method;
  2287. (*count)++;
  2288. }
  2289. }
  2290. return buf;
  2291. }
  2292. /**
  2293. * eap_set_fast_reauth - Update fast_reauth setting
  2294. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2295. * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
  2296. */
  2297. void eap_set_fast_reauth(struct eap_sm *sm, int enabled)
  2298. {
  2299. sm->fast_reauth = enabled;
  2300. }
  2301. /**
  2302. * eap_set_workaround - Update EAP workarounds setting
  2303. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2304. * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
  2305. */
  2306. void eap_set_workaround(struct eap_sm *sm, unsigned int workaround)
  2307. {
  2308. sm->workaround = workaround;
  2309. }
  2310. /**
  2311. * eap_get_config - Get current network configuration
  2312. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2313. * Returns: Pointer to the current network configuration or %NULL if not found
  2314. *
  2315. * EAP peer methods should avoid using this function if they can use other
  2316. * access functions, like eap_get_config_identity() and
  2317. * eap_get_config_password(), that do not require direct access to
  2318. * struct eap_peer_config.
  2319. */
  2320. struct eap_peer_config * eap_get_config(struct eap_sm *sm)
  2321. {
  2322. return sm->eapol_cb->get_config(sm->eapol_ctx);
  2323. }
  2324. /**
  2325. * eap_get_config_identity - Get identity from the network configuration
  2326. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2327. * @len: Buffer for the length of the identity
  2328. * Returns: Pointer to the identity or %NULL if not found
  2329. */
  2330. const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
  2331. {
  2332. struct eap_peer_config *config = eap_get_config(sm);
  2333. if (config == NULL)
  2334. return NULL;
  2335. *len = config->identity_len;
  2336. return config->identity;
  2337. }
  2338. static int eap_get_ext_password(struct eap_sm *sm,
  2339. struct eap_peer_config *config)
  2340. {
  2341. char *name;
  2342. if (config->password == NULL)
  2343. return -1;
  2344. name = os_zalloc(config->password_len + 1);
  2345. if (name == NULL)
  2346. return -1;
  2347. os_memcpy(name, config->password, config->password_len);
  2348. ext_password_free(sm->ext_pw_buf);
  2349. sm->ext_pw_buf = ext_password_get(sm->ext_pw, name);
  2350. os_free(name);
  2351. return sm->ext_pw_buf == NULL ? -1 : 0;
  2352. }
  2353. /**
  2354. * eap_get_config_password - Get password from the network configuration
  2355. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2356. * @len: Buffer for the length of the password
  2357. * Returns: Pointer to the password or %NULL if not found
  2358. */
  2359. const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
  2360. {
  2361. struct eap_peer_config *config = eap_get_config(sm);
  2362. if (config == NULL)
  2363. return NULL;
  2364. if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
  2365. if (eap_get_ext_password(sm, config) < 0)
  2366. return NULL;
  2367. *len = wpabuf_len(sm->ext_pw_buf);
  2368. return wpabuf_head(sm->ext_pw_buf);
  2369. }
  2370. *len = config->password_len;
  2371. return config->password;
  2372. }
  2373. /**
  2374. * eap_get_config_password2 - Get password from the network configuration
  2375. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2376. * @len: Buffer for the length of the password
  2377. * @hash: Buffer for returning whether the password is stored as a
  2378. * NtPasswordHash instead of plaintext password; can be %NULL if this
  2379. * information is not needed
  2380. * Returns: Pointer to the password or %NULL if not found
  2381. */
  2382. const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
  2383. {
  2384. struct eap_peer_config *config = eap_get_config(sm);
  2385. if (config == NULL)
  2386. return NULL;
  2387. if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
  2388. if (eap_get_ext_password(sm, config) < 0)
  2389. return NULL;
  2390. if (hash)
  2391. *hash = 0;
  2392. *len = wpabuf_len(sm->ext_pw_buf);
  2393. return wpabuf_head(sm->ext_pw_buf);
  2394. }
  2395. *len = config->password_len;
  2396. if (hash)
  2397. *hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH);
  2398. return config->password;
  2399. }
  2400. /**
  2401. * eap_get_config_new_password - Get new password from network configuration
  2402. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2403. * @len: Buffer for the length of the new password
  2404. * Returns: Pointer to the new password or %NULL if not found
  2405. */
  2406. const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
  2407. {
  2408. struct eap_peer_config *config = eap_get_config(sm);
  2409. if (config == NULL)
  2410. return NULL;
  2411. *len = config->new_password_len;
  2412. return config->new_password;
  2413. }
  2414. /**
  2415. * eap_get_config_otp - Get one-time password from the network configuration
  2416. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2417. * @len: Buffer for the length of the one-time password
  2418. * Returns: Pointer to the one-time password or %NULL if not found
  2419. */
  2420. const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len)
  2421. {
  2422. struct eap_peer_config *config = eap_get_config(sm);
  2423. if (config == NULL)
  2424. return NULL;
  2425. *len = config->otp_len;
  2426. return config->otp;
  2427. }
  2428. /**
  2429. * eap_clear_config_otp - Clear used one-time password
  2430. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2431. *
  2432. * This function clears a used one-time password (OTP) from the current network
  2433. * configuration. This should be called when the OTP has been used and is not
  2434. * needed anymore.
  2435. */
  2436. void eap_clear_config_otp(struct eap_sm *sm)
  2437. {
  2438. struct eap_peer_config *config = eap_get_config(sm);
  2439. if (config == NULL)
  2440. return;
  2441. os_memset(config->otp, 0, config->otp_len);
  2442. os_free(config->otp);
  2443. config->otp = NULL;
  2444. config->otp_len = 0;
  2445. }
  2446. /**
  2447. * eap_get_config_phase1 - Get phase1 data from the network configuration
  2448. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2449. * Returns: Pointer to the phase1 data or %NULL if not found
  2450. */
  2451. const char * eap_get_config_phase1(struct eap_sm *sm)
  2452. {
  2453. struct eap_peer_config *config = eap_get_config(sm);
  2454. if (config == NULL)
  2455. return NULL;
  2456. return config->phase1;
  2457. }
  2458. /**
  2459. * eap_get_config_phase2 - Get phase2 data from the network configuration
  2460. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2461. * Returns: Pointer to the phase1 data or %NULL if not found
  2462. */
  2463. const char * eap_get_config_phase2(struct eap_sm *sm)
  2464. {
  2465. struct eap_peer_config *config = eap_get_config(sm);
  2466. if (config == NULL)
  2467. return NULL;
  2468. return config->phase2;
  2469. }
  2470. int eap_get_config_fragment_size(struct eap_sm *sm)
  2471. {
  2472. struct eap_peer_config *config = eap_get_config(sm);
  2473. if (config == NULL)
  2474. return -1;
  2475. return config->fragment_size;
  2476. }
  2477. /**
  2478. * eap_key_available - Get key availability (eapKeyAvailable variable)
  2479. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2480. * Returns: 1 if EAP keying material is available, 0 if not
  2481. */
  2482. int eap_key_available(struct eap_sm *sm)
  2483. {
  2484. return sm ? sm->eapKeyAvailable : 0;
  2485. }
  2486. /**
  2487. * eap_notify_success - Notify EAP state machine about external success trigger
  2488. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2489. *
  2490. * This function is called when external event, e.g., successful completion of
  2491. * WPA-PSK key handshake, is indicating that EAP state machine should move to
  2492. * success state. This is mainly used with security modes that do not use EAP
  2493. * state machine (e.g., WPA-PSK).
  2494. */
  2495. void eap_notify_success(struct eap_sm *sm)
  2496. {
  2497. if (sm) {
  2498. sm->decision = DECISION_COND_SUCC;
  2499. sm->EAP_state = EAP_SUCCESS;
  2500. }
  2501. }
  2502. /**
  2503. * eap_notify_lower_layer_success - Notification of lower layer success
  2504. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2505. *
  2506. * Notify EAP state machines that a lower layer has detected a successful
  2507. * authentication. This is used to recover from dropped EAP-Success messages.
  2508. */
  2509. void eap_notify_lower_layer_success(struct eap_sm *sm)
  2510. {
  2511. if (sm == NULL)
  2512. return;
  2513. if (eapol_get_bool(sm, EAPOL_eapSuccess) ||
  2514. sm->decision == DECISION_FAIL ||
  2515. (sm->methodState != METHOD_MAY_CONT &&
  2516. sm->methodState != METHOD_DONE))
  2517. return;
  2518. if (sm->eapKeyData != NULL)
  2519. sm->eapKeyAvailable = TRUE;
  2520. eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
  2521. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
  2522. "EAP authentication completed successfully (based on lower "
  2523. "layer success)");
  2524. }
  2525. /**
  2526. * eap_get_eapSessionId - Get Session-Id from EAP state machine
  2527. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2528. * @len: Pointer to variable that will be set to number of bytes in the session
  2529. * Returns: Pointer to the EAP Session-Id or %NULL on failure
  2530. *
  2531. * Fetch EAP Session-Id from the EAP state machine. The Session-Id is available
  2532. * only after a successful authentication. EAP state machine continues to manage
  2533. * the Session-Id and the caller must not change or free the returned data.
  2534. */
  2535. const u8 * eap_get_eapSessionId(struct eap_sm *sm, size_t *len)
  2536. {
  2537. if (sm == NULL || sm->eapSessionId == NULL) {
  2538. *len = 0;
  2539. return NULL;
  2540. }
  2541. *len = sm->eapSessionIdLen;
  2542. return sm->eapSessionId;
  2543. }
  2544. /**
  2545. * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
  2546. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2547. * @len: Pointer to variable that will be set to number of bytes in the key
  2548. * Returns: Pointer to the EAP keying data or %NULL on failure
  2549. *
  2550. * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
  2551. * key is available only after a successful authentication. EAP state machine
  2552. * continues to manage the key data and the caller must not change or free the
  2553. * returned data.
  2554. */
  2555. const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len)
  2556. {
  2557. if (sm == NULL || sm->eapKeyData == NULL) {
  2558. *len = 0;
  2559. return NULL;
  2560. }
  2561. *len = sm->eapKeyDataLen;
  2562. return sm->eapKeyData;
  2563. }
  2564. /**
  2565. * eap_get_eapKeyData - Get EAP response data
  2566. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2567. * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
  2568. *
  2569. * Fetch EAP response (eapRespData) from the EAP state machine. This data is
  2570. * available when EAP state machine has processed an incoming EAP request. The
  2571. * EAP state machine does not maintain a reference to the response after this
  2572. * function is called and the caller is responsible for freeing the data.
  2573. */
  2574. struct wpabuf * eap_get_eapRespData(struct eap_sm *sm)
  2575. {
  2576. struct wpabuf *resp;
  2577. if (sm == NULL || sm->eapRespData == NULL)
  2578. return NULL;
  2579. resp = sm->eapRespData;
  2580. sm->eapRespData = NULL;
  2581. return resp;
  2582. }
  2583. /**
  2584. * eap_sm_register_scard_ctx - Notification of smart card context
  2585. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2586. * @ctx: Context data for smart card operations
  2587. *
  2588. * Notify EAP state machines of context data for smart card operations. This
  2589. * context data will be used as a parameter for scard_*() functions.
  2590. */
  2591. void eap_register_scard_ctx(struct eap_sm *sm, void *ctx)
  2592. {
  2593. if (sm)
  2594. sm->scard_ctx = ctx;
  2595. }
  2596. /**
  2597. * eap_set_config_blob - Set or add a named configuration blob
  2598. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2599. * @blob: New value for the blob
  2600. *
  2601. * Adds a new configuration blob or replaces the current value of an existing
  2602. * blob.
  2603. */
  2604. void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob)
  2605. {
  2606. #ifndef CONFIG_NO_CONFIG_BLOBS
  2607. sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob);
  2608. #endif /* CONFIG_NO_CONFIG_BLOBS */
  2609. }
  2610. /**
  2611. * eap_get_config_blob - Get a named configuration blob
  2612. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2613. * @name: Name of the blob
  2614. * Returns: Pointer to blob data or %NULL if not found
  2615. */
  2616. const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm,
  2617. const char *name)
  2618. {
  2619. #ifndef CONFIG_NO_CONFIG_BLOBS
  2620. return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name);
  2621. #else /* CONFIG_NO_CONFIG_BLOBS */
  2622. return NULL;
  2623. #endif /* CONFIG_NO_CONFIG_BLOBS */
  2624. }
  2625. /**
  2626. * eap_set_force_disabled - Set force_disabled flag
  2627. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2628. * @disabled: 1 = EAP disabled, 0 = EAP enabled
  2629. *
  2630. * This function is used to force EAP state machine to be disabled when it is
  2631. * not in use (e.g., with WPA-PSK or plaintext connections).
  2632. */
  2633. void eap_set_force_disabled(struct eap_sm *sm, int disabled)
  2634. {
  2635. sm->force_disabled = disabled;
  2636. }
  2637. /**
  2638. * eap_set_external_sim - Set external_sim flag
  2639. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2640. * @external_sim: Whether external SIM/USIM processing is used
  2641. */
  2642. void eap_set_external_sim(struct eap_sm *sm, int external_sim)
  2643. {
  2644. sm->external_sim = external_sim;
  2645. }
  2646. /**
  2647. * eap_notify_pending - Notify that EAP method is ready to re-process a request
  2648. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2649. *
  2650. * An EAP method can perform a pending operation (e.g., to get a response from
  2651. * an external process). Once the response is available, this function can be
  2652. * used to request EAPOL state machine to retry delivering the previously
  2653. * received (and still unanswered) EAP request to EAP state machine.
  2654. */
  2655. void eap_notify_pending(struct eap_sm *sm)
  2656. {
  2657. sm->eapol_cb->notify_pending(sm->eapol_ctx);
  2658. }
  2659. /**
  2660. * eap_invalidate_cached_session - Mark cached session data invalid
  2661. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2662. */
  2663. void eap_invalidate_cached_session(struct eap_sm *sm)
  2664. {
  2665. if (sm)
  2666. eap_deinit_prev_method(sm, "invalidate");
  2667. }
  2668. int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf)
  2669. {
  2670. if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
  2671. os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
  2672. return 0; /* Not a WPS Enrollee */
  2673. if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL)
  2674. return 0; /* Not using PBC */
  2675. return 1;
  2676. }
  2677. int eap_is_wps_pin_enrollee(struct eap_peer_config *conf)
  2678. {
  2679. if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
  2680. os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
  2681. return 0; /* Not a WPS Enrollee */
  2682. if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL)
  2683. return 0; /* Not using PIN */
  2684. return 1;
  2685. }
  2686. void eap_sm_set_ext_pw_ctx(struct eap_sm *sm, struct ext_password_data *ext)
  2687. {
  2688. ext_password_free(sm->ext_pw_buf);
  2689. sm->ext_pw_buf = NULL;
  2690. sm->ext_pw = ext;
  2691. }
  2692. /**
  2693. * eap_set_anon_id - Set or add anonymous identity
  2694. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  2695. * @id: Anonymous identity (e.g., EAP-SIM pseudonym) or %NULL to clear
  2696. * @len: Length of anonymous identity in octets
  2697. */
  2698. void eap_set_anon_id(struct eap_sm *sm, const u8 *id, size_t len)
  2699. {
  2700. if (sm->eapol_cb->set_anon_id)
  2701. sm->eapol_cb->set_anon_id(sm->eapol_ctx, id, len);
  2702. }
  2703. int eap_peer_was_failure_expected(struct eap_sm *sm)
  2704. {
  2705. return sm->expected_failure;
  2706. }