eap.c 57 KB

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