eap.c 79 KB

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