eap.c 78 KB

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