eap.c 66 KB

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