eap_server.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  1. /*
  2. * hostapd / EAP Full Authenticator state machine (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 state machine is based on the full authenticator state machine defined
  9. * in RFC 4137. However, to support backend authentication in RADIUS
  10. * authentication server functionality, parts of backend authenticator (also
  11. * from RFC 4137) are mixed in. This functionality is enabled by setting
  12. * backend_auth configuration variable to TRUE.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "crypto/sha256.h"
  17. #include "eap_i.h"
  18. #include "state_machine.h"
  19. #include "common/wpa_ctrl.h"
  20. #define STATE_MACHINE_DATA struct eap_sm
  21. #define STATE_MACHINE_DEBUG_PREFIX "EAP"
  22. #define EAP_MAX_AUTH_ROUNDS 50
  23. static void eap_user_free(struct eap_user *user);
  24. /* EAP state machines are described in RFC 4137 */
  25. static int eap_sm_calculateTimeout(struct eap_sm *sm, int retransCount,
  26. int eapSRTT, int eapRTTVAR,
  27. int methodTimeout);
  28. static void eap_sm_parseEapResp(struct eap_sm *sm, const struct wpabuf *resp);
  29. static int eap_sm_getId(const struct wpabuf *data);
  30. static struct wpabuf * eap_sm_buildSuccess(struct eap_sm *sm, u8 id);
  31. static struct wpabuf * eap_sm_buildFailure(struct eap_sm *sm, u8 id);
  32. static int eap_sm_nextId(struct eap_sm *sm, int id);
  33. static void eap_sm_Policy_update(struct eap_sm *sm, const u8 *nak_list,
  34. size_t len);
  35. static EapType eap_sm_Policy_getNextMethod(struct eap_sm *sm, int *vendor);
  36. static int eap_sm_Policy_getDecision(struct eap_sm *sm);
  37. static Boolean eap_sm_Policy_doPickUp(struct eap_sm *sm, EapType method);
  38. static int eap_get_erp_send_reauth_start(struct eap_sm *sm)
  39. {
  40. if (sm->eapol_cb->get_erp_send_reauth_start)
  41. return sm->eapol_cb->get_erp_send_reauth_start(sm->eapol_ctx);
  42. return 0;
  43. }
  44. static const char * eap_get_erp_domain(struct eap_sm *sm)
  45. {
  46. if (sm->eapol_cb->get_erp_domain)
  47. return sm->eapol_cb->get_erp_domain(sm->eapol_ctx);
  48. return NULL;
  49. }
  50. #ifdef CONFIG_ERP
  51. static struct eap_server_erp_key * eap_erp_get_key(struct eap_sm *sm,
  52. const char *keyname)
  53. {
  54. if (sm->eapol_cb->erp_get_key)
  55. return sm->eapol_cb->erp_get_key(sm->eapol_ctx, keyname);
  56. return NULL;
  57. }
  58. static int eap_erp_add_key(struct eap_sm *sm, struct eap_server_erp_key *erp)
  59. {
  60. if (sm->eapol_cb->erp_add_key)
  61. return sm->eapol_cb->erp_add_key(sm->eapol_ctx, erp);
  62. return -1;
  63. }
  64. #endif /* CONFIG_ERP */
  65. static struct wpabuf * eap_sm_buildInitiateReauthStart(struct eap_sm *sm,
  66. u8 id)
  67. {
  68. const char *domain;
  69. size_t plen = 1;
  70. struct wpabuf *msg;
  71. size_t domain_len = 0;
  72. domain = eap_get_erp_domain(sm);
  73. if (domain) {
  74. domain_len = os_strlen(domain);
  75. plen += 2 + domain_len;
  76. }
  77. msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_ERP_TYPE_REAUTH_START, plen,
  78. EAP_CODE_INITIATE, id);
  79. if (msg == NULL)
  80. return NULL;
  81. wpabuf_put_u8(msg, 0); /* Reserved */
  82. if (domain) {
  83. /* Domain name TLV */
  84. wpabuf_put_u8(msg, EAP_ERP_TLV_DOMAIN_NAME);
  85. wpabuf_put_u8(msg, domain_len);
  86. wpabuf_put_data(msg, domain, domain_len);
  87. }
  88. return msg;
  89. }
  90. static int eap_copy_buf(struct wpabuf **dst, const struct wpabuf *src)
  91. {
  92. if (src == NULL)
  93. return -1;
  94. wpabuf_free(*dst);
  95. *dst = wpabuf_dup(src);
  96. return *dst ? 0 : -1;
  97. }
  98. static int eap_copy_data(u8 **dst, size_t *dst_len,
  99. const u8 *src, size_t src_len)
  100. {
  101. if (src == NULL)
  102. return -1;
  103. os_free(*dst);
  104. *dst = os_malloc(src_len);
  105. if (*dst) {
  106. os_memcpy(*dst, src, src_len);
  107. *dst_len = src_len;
  108. return 0;
  109. } else {
  110. *dst_len = 0;
  111. return -1;
  112. }
  113. }
  114. #define EAP_COPY(dst, src) \
  115. eap_copy_data((dst), (dst ## Len), (src), (src ## Len))
  116. /**
  117. * eap_user_get - Fetch user information from the database
  118. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  119. * @identity: Identity (User-Name) of the user
  120. * @identity_len: Length of identity in bytes
  121. * @phase2: 0 = EAP phase1 user, 1 = EAP phase2 (tunneled) user
  122. * Returns: 0 on success, or -1 on failure
  123. *
  124. * This function is used to fetch user information for EAP. The user will be
  125. * selected based on the specified identity. sm->user and
  126. * sm->user_eap_method_index are updated for the new user when a matching user
  127. * is found. sm->user can be used to get user information (e.g., password).
  128. */
  129. int eap_user_get(struct eap_sm *sm, const u8 *identity, size_t identity_len,
  130. int phase2)
  131. {
  132. struct eap_user *user;
  133. if (sm == NULL || sm->eapol_cb == NULL ||
  134. sm->eapol_cb->get_eap_user == NULL)
  135. return -1;
  136. eap_user_free(sm->user);
  137. sm->user = NULL;
  138. user = os_zalloc(sizeof(*user));
  139. if (user == NULL)
  140. return -1;
  141. if (sm->eapol_cb->get_eap_user(sm->eapol_ctx, identity,
  142. identity_len, phase2, user) != 0) {
  143. eap_user_free(user);
  144. return -1;
  145. }
  146. sm->user = user;
  147. sm->user_eap_method_index = 0;
  148. return 0;
  149. }
  150. void eap_log_msg(struct eap_sm *sm, const char *fmt, ...)
  151. {
  152. va_list ap;
  153. char *buf;
  154. int buflen;
  155. if (sm == NULL || sm->eapol_cb == NULL || sm->eapol_cb->log_msg == NULL)
  156. return;
  157. va_start(ap, fmt);
  158. buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
  159. va_end(ap);
  160. buf = os_malloc(buflen);
  161. if (buf == NULL)
  162. return;
  163. va_start(ap, fmt);
  164. vsnprintf(buf, buflen, fmt, ap);
  165. va_end(ap);
  166. sm->eapol_cb->log_msg(sm->eapol_ctx, buf);
  167. os_free(buf);
  168. }
  169. SM_STATE(EAP, DISABLED)
  170. {
  171. SM_ENTRY(EAP, DISABLED);
  172. sm->num_rounds = 0;
  173. }
  174. SM_STATE(EAP, INITIALIZE)
  175. {
  176. SM_ENTRY(EAP, INITIALIZE);
  177. if (sm->eap_if.eapRestart && !sm->eap_server && sm->identity) {
  178. /*
  179. * Need to allow internal Identity method to be used instead
  180. * of passthrough at the beginning of reauthentication.
  181. */
  182. eap_server_clear_identity(sm);
  183. }
  184. sm->try_initiate_reauth = FALSE;
  185. sm->currentId = -1;
  186. sm->eap_if.eapSuccess = FALSE;
  187. sm->eap_if.eapFail = FALSE;
  188. sm->eap_if.eapTimeout = FALSE;
  189. bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
  190. sm->eap_if.eapKeyData = NULL;
  191. sm->eap_if.eapKeyDataLen = 0;
  192. os_free(sm->eap_if.eapSessionId);
  193. sm->eap_if.eapSessionId = NULL;
  194. sm->eap_if.eapSessionIdLen = 0;
  195. sm->eap_if.eapKeyAvailable = FALSE;
  196. sm->eap_if.eapRestart = FALSE;
  197. /*
  198. * This is not defined in RFC 4137, but method state needs to be
  199. * reseted here so that it does not remain in success state when
  200. * re-authentication starts.
  201. */
  202. if (sm->m && sm->eap_method_priv) {
  203. sm->m->reset(sm, sm->eap_method_priv);
  204. sm->eap_method_priv = NULL;
  205. }
  206. sm->m = NULL;
  207. sm->user_eap_method_index = 0;
  208. if (sm->backend_auth) {
  209. sm->currentMethod = EAP_TYPE_NONE;
  210. /* parse rxResp, respId, respMethod */
  211. eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
  212. if (sm->rxResp) {
  213. sm->currentId = sm->respId;
  214. }
  215. }
  216. sm->num_rounds = 0;
  217. sm->method_pending = METHOD_PENDING_NONE;
  218. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
  219. MACSTR, MAC2STR(sm->peer_addr));
  220. }
  221. SM_STATE(EAP, PICK_UP_METHOD)
  222. {
  223. SM_ENTRY(EAP, PICK_UP_METHOD);
  224. if (eap_sm_Policy_doPickUp(sm, sm->respMethod)) {
  225. sm->currentMethod = sm->respMethod;
  226. if (sm->m && sm->eap_method_priv) {
  227. sm->m->reset(sm, sm->eap_method_priv);
  228. sm->eap_method_priv = NULL;
  229. }
  230. sm->m = eap_server_get_eap_method(EAP_VENDOR_IETF,
  231. sm->currentMethod);
  232. if (sm->m && sm->m->initPickUp) {
  233. sm->eap_method_priv = sm->m->initPickUp(sm);
  234. if (sm->eap_method_priv == NULL) {
  235. wpa_printf(MSG_DEBUG, "EAP: Failed to "
  236. "initialize EAP method %d",
  237. sm->currentMethod);
  238. sm->m = NULL;
  239. sm->currentMethod = EAP_TYPE_NONE;
  240. }
  241. } else {
  242. sm->m = NULL;
  243. sm->currentMethod = EAP_TYPE_NONE;
  244. }
  245. }
  246. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
  247. "method=%u", sm->currentMethod);
  248. }
  249. SM_STATE(EAP, IDLE)
  250. {
  251. SM_ENTRY(EAP, IDLE);
  252. sm->eap_if.retransWhile = eap_sm_calculateTimeout(
  253. sm, sm->retransCount, sm->eap_if.eapSRTT, sm->eap_if.eapRTTVAR,
  254. sm->methodTimeout);
  255. }
  256. SM_STATE(EAP, RETRANSMIT)
  257. {
  258. SM_ENTRY(EAP, RETRANSMIT);
  259. sm->retransCount++;
  260. if (sm->retransCount <= sm->MaxRetrans && sm->lastReqData) {
  261. if (eap_copy_buf(&sm->eap_if.eapReqData, sm->lastReqData) == 0)
  262. sm->eap_if.eapReq = TRUE;
  263. }
  264. }
  265. SM_STATE(EAP, RECEIVED)
  266. {
  267. SM_ENTRY(EAP, RECEIVED);
  268. /* parse rxResp, respId, respMethod */
  269. eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
  270. sm->num_rounds++;
  271. }
  272. SM_STATE(EAP, DISCARD)
  273. {
  274. SM_ENTRY(EAP, DISCARD);
  275. sm->eap_if.eapResp = FALSE;
  276. sm->eap_if.eapNoReq = TRUE;
  277. }
  278. SM_STATE(EAP, SEND_REQUEST)
  279. {
  280. SM_ENTRY(EAP, SEND_REQUEST);
  281. sm->retransCount = 0;
  282. if (sm->eap_if.eapReqData) {
  283. if (eap_copy_buf(&sm->lastReqData, sm->eap_if.eapReqData) == 0)
  284. {
  285. sm->eap_if.eapResp = FALSE;
  286. sm->eap_if.eapReq = TRUE;
  287. } else {
  288. sm->eap_if.eapResp = FALSE;
  289. sm->eap_if.eapReq = FALSE;
  290. }
  291. } else {
  292. wpa_printf(MSG_INFO, "EAP: SEND_REQUEST - no eapReqData");
  293. sm->eap_if.eapResp = FALSE;
  294. sm->eap_if.eapReq = FALSE;
  295. sm->eap_if.eapNoReq = TRUE;
  296. }
  297. }
  298. SM_STATE(EAP, INTEGRITY_CHECK)
  299. {
  300. SM_ENTRY(EAP, INTEGRITY_CHECK);
  301. if (!eap_hdr_len_valid(sm->eap_if.eapRespData, 1)) {
  302. sm->ignore = TRUE;
  303. return;
  304. }
  305. if (sm->m->check) {
  306. sm->ignore = sm->m->check(sm, sm->eap_method_priv,
  307. sm->eap_if.eapRespData);
  308. }
  309. }
  310. SM_STATE(EAP, METHOD_REQUEST)
  311. {
  312. SM_ENTRY(EAP, METHOD_REQUEST);
  313. if (sm->m == NULL) {
  314. wpa_printf(MSG_DEBUG, "EAP: method not initialized");
  315. return;
  316. }
  317. sm->currentId = eap_sm_nextId(sm, sm->currentId);
  318. wpa_printf(MSG_DEBUG, "EAP: building EAP-Request: Identifier %d",
  319. sm->currentId);
  320. sm->lastId = sm->currentId;
  321. wpabuf_free(sm->eap_if.eapReqData);
  322. sm->eap_if.eapReqData = sm->m->buildReq(sm, sm->eap_method_priv,
  323. sm->currentId);
  324. if (sm->m->getTimeout)
  325. sm->methodTimeout = sm->m->getTimeout(sm, sm->eap_method_priv);
  326. else
  327. sm->methodTimeout = 0;
  328. }
  329. static void eap_server_erp_init(struct eap_sm *sm)
  330. {
  331. #ifdef CONFIG_ERP
  332. u8 *emsk = NULL;
  333. size_t emsk_len = 0;
  334. u8 EMSKname[EAP_EMSK_NAME_LEN];
  335. u8 len[2];
  336. const char *domain;
  337. size_t domain_len, nai_buf_len;
  338. struct eap_server_erp_key *erp = NULL;
  339. int pos;
  340. domain = eap_get_erp_domain(sm);
  341. if (!domain)
  342. return;
  343. domain_len = os_strlen(domain);
  344. nai_buf_len = 2 * EAP_EMSK_NAME_LEN + 1 + domain_len;
  345. if (nai_buf_len > 253) {
  346. /*
  347. * keyName-NAI has a maximum length of 253 octet to fit in
  348. * RADIUS attributes.
  349. */
  350. wpa_printf(MSG_DEBUG,
  351. "EAP: Too long realm for ERP keyName-NAI maximum length");
  352. return;
  353. }
  354. nai_buf_len++; /* null termination */
  355. erp = os_zalloc(sizeof(*erp) + nai_buf_len);
  356. if (erp == NULL)
  357. goto fail;
  358. erp->recv_seq = (u32) -1;
  359. emsk = sm->m->get_emsk(sm, sm->eap_method_priv, &emsk_len);
  360. if (!emsk || emsk_len == 0 || emsk_len > ERP_MAX_KEY_LEN) {
  361. wpa_printf(MSG_DEBUG,
  362. "EAP: No suitable EMSK available for ERP");
  363. goto fail;
  364. }
  365. wpa_hexdump_key(MSG_DEBUG, "EAP: EMSK", emsk, emsk_len);
  366. WPA_PUT_BE16(len, 8);
  367. if (hmac_sha256_kdf(sm->eap_if.eapSessionId, sm->eap_if.eapSessionIdLen,
  368. "EMSK", len, sizeof(len),
  369. EMSKname, EAP_EMSK_NAME_LEN) < 0) {
  370. wpa_printf(MSG_DEBUG, "EAP: Could not derive EMSKname");
  371. goto fail;
  372. }
  373. wpa_hexdump(MSG_DEBUG, "EAP: EMSKname", EMSKname, EAP_EMSK_NAME_LEN);
  374. pos = wpa_snprintf_hex(erp->keyname_nai, nai_buf_len,
  375. EMSKname, EAP_EMSK_NAME_LEN);
  376. erp->keyname_nai[pos] = '@';
  377. os_memcpy(&erp->keyname_nai[pos + 1], domain, domain_len);
  378. WPA_PUT_BE16(len, emsk_len);
  379. if (hmac_sha256_kdf(emsk, emsk_len,
  380. "EAP Re-authentication Root Key@ietf.org",
  381. len, sizeof(len), erp->rRK, emsk_len) < 0) {
  382. wpa_printf(MSG_DEBUG, "EAP: Could not derive rRK for ERP");
  383. goto fail;
  384. }
  385. erp->rRK_len = emsk_len;
  386. wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rRK", erp->rRK, erp->rRK_len);
  387. if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
  388. "EAP Re-authentication Integrity Key@ietf.org",
  389. len, sizeof(len), erp->rIK, erp->rRK_len) < 0) {
  390. wpa_printf(MSG_DEBUG, "EAP: Could not derive rIK for ERP");
  391. goto fail;
  392. }
  393. erp->rIK_len = erp->rRK_len;
  394. wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rIK", erp->rIK, erp->rIK_len);
  395. if (eap_erp_add_key(sm, erp) == 0) {
  396. wpa_printf(MSG_DEBUG, "EAP: Stored ERP keys %s",
  397. erp->keyname_nai);
  398. erp = NULL;
  399. }
  400. fail:
  401. bin_clear_free(emsk, emsk_len);
  402. bin_clear_free(erp, sizeof(*erp));
  403. #endif /* CONFIG_ERP */
  404. }
  405. SM_STATE(EAP, METHOD_RESPONSE)
  406. {
  407. SM_ENTRY(EAP, METHOD_RESPONSE);
  408. if (!eap_hdr_len_valid(sm->eap_if.eapRespData, 1))
  409. return;
  410. sm->m->process(sm, sm->eap_method_priv, sm->eap_if.eapRespData);
  411. if (sm->m->isDone(sm, sm->eap_method_priv)) {
  412. eap_sm_Policy_update(sm, NULL, 0);
  413. bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
  414. if (sm->m->getKey) {
  415. sm->eap_if.eapKeyData = sm->m->getKey(
  416. sm, sm->eap_method_priv,
  417. &sm->eap_if.eapKeyDataLen);
  418. } else {
  419. sm->eap_if.eapKeyData = NULL;
  420. sm->eap_if.eapKeyDataLen = 0;
  421. }
  422. os_free(sm->eap_if.eapSessionId);
  423. sm->eap_if.eapSessionId = NULL;
  424. if (sm->m->getSessionId) {
  425. sm->eap_if.eapSessionId = sm->m->getSessionId(
  426. sm, sm->eap_method_priv,
  427. &sm->eap_if.eapSessionIdLen);
  428. wpa_hexdump(MSG_DEBUG, "EAP: Session-Id",
  429. sm->eap_if.eapSessionId,
  430. sm->eap_if.eapSessionIdLen);
  431. }
  432. if (sm->erp && sm->m->get_emsk && sm->eap_if.eapSessionId)
  433. eap_server_erp_init(sm);
  434. sm->methodState = METHOD_END;
  435. } else {
  436. sm->methodState = METHOD_CONTINUE;
  437. }
  438. }
  439. SM_STATE(EAP, PROPOSE_METHOD)
  440. {
  441. int vendor;
  442. EapType type;
  443. SM_ENTRY(EAP, PROPOSE_METHOD);
  444. sm->try_initiate_reauth = FALSE;
  445. try_another_method:
  446. type = eap_sm_Policy_getNextMethod(sm, &vendor);
  447. if (vendor == EAP_VENDOR_IETF)
  448. sm->currentMethod = type;
  449. else
  450. sm->currentMethod = EAP_TYPE_EXPANDED;
  451. if (sm->m && sm->eap_method_priv) {
  452. sm->m->reset(sm, sm->eap_method_priv);
  453. sm->eap_method_priv = NULL;
  454. }
  455. sm->m = eap_server_get_eap_method(vendor, type);
  456. if (sm->m) {
  457. sm->eap_method_priv = sm->m->init(sm);
  458. if (sm->eap_method_priv == NULL) {
  459. wpa_printf(MSG_DEBUG, "EAP: Failed to initialize EAP "
  460. "method %d", sm->currentMethod);
  461. sm->m = NULL;
  462. sm->currentMethod = EAP_TYPE_NONE;
  463. goto try_another_method;
  464. }
  465. }
  466. if (sm->m == NULL) {
  467. wpa_printf(MSG_DEBUG, "EAP: Could not find suitable EAP method");
  468. eap_log_msg(sm, "Could not find suitable EAP method");
  469. sm->decision = DECISION_FAILURE;
  470. return;
  471. }
  472. if (sm->currentMethod == EAP_TYPE_IDENTITY ||
  473. sm->currentMethod == EAP_TYPE_NOTIFICATION)
  474. sm->methodState = METHOD_CONTINUE;
  475. else
  476. sm->methodState = METHOD_PROPOSED;
  477. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
  478. "vendor=%u method=%u", vendor, sm->currentMethod);
  479. eap_log_msg(sm, "Propose EAP method vendor=%u method=%u",
  480. vendor, sm->currentMethod);
  481. }
  482. SM_STATE(EAP, NAK)
  483. {
  484. const struct eap_hdr *nak;
  485. size_t len = 0;
  486. const u8 *pos;
  487. const u8 *nak_list = NULL;
  488. SM_ENTRY(EAP, NAK);
  489. if (sm->eap_method_priv) {
  490. sm->m->reset(sm, sm->eap_method_priv);
  491. sm->eap_method_priv = NULL;
  492. }
  493. sm->m = NULL;
  494. if (!eap_hdr_len_valid(sm->eap_if.eapRespData, 1))
  495. return;
  496. nak = wpabuf_head(sm->eap_if.eapRespData);
  497. if (nak && wpabuf_len(sm->eap_if.eapRespData) > sizeof(*nak)) {
  498. len = be_to_host16(nak->length);
  499. if (len > wpabuf_len(sm->eap_if.eapRespData))
  500. len = wpabuf_len(sm->eap_if.eapRespData);
  501. pos = (const u8 *) (nak + 1);
  502. len -= sizeof(*nak);
  503. if (*pos == EAP_TYPE_NAK) {
  504. pos++;
  505. len--;
  506. nak_list = pos;
  507. }
  508. }
  509. eap_sm_Policy_update(sm, nak_list, len);
  510. }
  511. SM_STATE(EAP, SELECT_ACTION)
  512. {
  513. SM_ENTRY(EAP, SELECT_ACTION);
  514. sm->decision = eap_sm_Policy_getDecision(sm);
  515. }
  516. SM_STATE(EAP, TIMEOUT_FAILURE)
  517. {
  518. SM_ENTRY(EAP, TIMEOUT_FAILURE);
  519. sm->eap_if.eapTimeout = TRUE;
  520. }
  521. SM_STATE(EAP, FAILURE)
  522. {
  523. SM_ENTRY(EAP, FAILURE);
  524. wpabuf_free(sm->eap_if.eapReqData);
  525. sm->eap_if.eapReqData = eap_sm_buildFailure(sm, sm->currentId);
  526. wpabuf_free(sm->lastReqData);
  527. sm->lastReqData = NULL;
  528. sm->eap_if.eapFail = TRUE;
  529. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
  530. MACSTR, MAC2STR(sm->peer_addr));
  531. }
  532. SM_STATE(EAP, SUCCESS)
  533. {
  534. SM_ENTRY(EAP, SUCCESS);
  535. wpabuf_free(sm->eap_if.eapReqData);
  536. sm->eap_if.eapReqData = eap_sm_buildSuccess(sm, sm->currentId);
  537. wpabuf_free(sm->lastReqData);
  538. sm->lastReqData = NULL;
  539. if (sm->eap_if.eapKeyData)
  540. sm->eap_if.eapKeyAvailable = TRUE;
  541. sm->eap_if.eapSuccess = TRUE;
  542. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
  543. MACSTR, MAC2STR(sm->peer_addr));
  544. }
  545. SM_STATE(EAP, INITIATE_REAUTH_START)
  546. {
  547. SM_ENTRY(EAP, INITIATE_REAUTH_START);
  548. sm->initiate_reauth_start_sent = TRUE;
  549. sm->try_initiate_reauth = TRUE;
  550. sm->currentId = eap_sm_nextId(sm, sm->currentId);
  551. wpa_printf(MSG_DEBUG,
  552. "EAP: building EAP-Initiate-Re-auth-Start: Identifier %d",
  553. sm->currentId);
  554. sm->lastId = sm->currentId;
  555. wpabuf_free(sm->eap_if.eapReqData);
  556. sm->eap_if.eapReqData = eap_sm_buildInitiateReauthStart(sm,
  557. sm->currentId);
  558. wpabuf_free(sm->lastReqData);
  559. sm->lastReqData = NULL;
  560. }
  561. #ifdef CONFIG_ERP
  562. static void erp_send_finish_reauth(struct eap_sm *sm,
  563. struct eap_server_erp_key *erp, u8 id,
  564. u8 flags, u16 seq, const char *nai)
  565. {
  566. size_t plen;
  567. struct wpabuf *msg;
  568. u8 hash[SHA256_MAC_LEN];
  569. size_t hash_len;
  570. u8 seed[4];
  571. if (erp) {
  572. switch (erp->cryptosuite) {
  573. case EAP_ERP_CS_HMAC_SHA256_256:
  574. hash_len = 32;
  575. break;
  576. case EAP_ERP_CS_HMAC_SHA256_128:
  577. hash_len = 16;
  578. break;
  579. default:
  580. return;
  581. }
  582. } else
  583. hash_len = 0;
  584. plen = 1 + 2 + 2 + os_strlen(nai);
  585. if (hash_len)
  586. plen += 1 + hash_len;
  587. msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_ERP_TYPE_REAUTH, plen,
  588. EAP_CODE_FINISH, id);
  589. if (msg == NULL)
  590. return;
  591. wpabuf_put_u8(msg, flags);
  592. wpabuf_put_be16(msg, seq);
  593. wpabuf_put_u8(msg, EAP_ERP_TLV_KEYNAME_NAI);
  594. wpabuf_put_u8(msg, os_strlen(nai));
  595. wpabuf_put_str(msg, nai);
  596. if (erp) {
  597. wpabuf_put_u8(msg, erp->cryptosuite);
  598. if (hmac_sha256(erp->rIK, erp->rIK_len,
  599. wpabuf_head(msg), wpabuf_len(msg), hash) < 0) {
  600. wpabuf_free(msg);
  601. return;
  602. }
  603. wpabuf_put_data(msg, hash, hash_len);
  604. }
  605. wpa_printf(MSG_DEBUG, "EAP: Send EAP-Finish/Re-auth (%s)",
  606. flags & 0x80 ? "failure" : "success");
  607. sm->lastId = sm->currentId;
  608. sm->currentId = id;
  609. wpabuf_free(sm->eap_if.eapReqData);
  610. sm->eap_if.eapReqData = msg;
  611. wpabuf_free(sm->lastReqData);
  612. sm->lastReqData = NULL;
  613. if (flags & 0x80) {
  614. sm->eap_if.eapFail = TRUE;
  615. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
  616. MACSTR, MAC2STR(sm->peer_addr));
  617. return;
  618. }
  619. bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
  620. sm->eap_if.eapKeyDataLen = 0;
  621. sm->eap_if.eapKeyData = os_malloc(erp->rRK_len);
  622. if (!sm->eap_if.eapKeyData)
  623. return;
  624. WPA_PUT_BE16(seed, seq);
  625. WPA_PUT_BE16(&seed[2], erp->rRK_len);
  626. if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
  627. "Re-authentication Master Session Key@ietf.org",
  628. seed, sizeof(seed),
  629. sm->eap_if.eapKeyData, erp->rRK_len) < 0) {
  630. wpa_printf(MSG_DEBUG, "EAP: Could not derive rMSK for ERP");
  631. bin_clear_free(sm->eap_if.eapKeyData, erp->rRK_len);
  632. sm->eap_if.eapKeyData = NULL;
  633. return;
  634. }
  635. sm->eap_if.eapKeyDataLen = erp->rRK_len;
  636. sm->eap_if.eapKeyAvailable = TRUE;
  637. wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rMSK",
  638. sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
  639. sm->eap_if.eapSuccess = TRUE;
  640. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
  641. MACSTR, MAC2STR(sm->peer_addr));
  642. }
  643. SM_STATE(EAP, INITIATE_RECEIVED)
  644. {
  645. const u8 *pos, *end, *start, *tlvs, *hdr;
  646. const struct eap_hdr *ehdr;
  647. size_t len;
  648. u8 flags;
  649. u16 seq;
  650. char nai[254];
  651. struct eap_server_erp_key *erp;
  652. int max_len;
  653. u8 hash[SHA256_MAC_LEN];
  654. size_t hash_len;
  655. struct erp_tlvs parse;
  656. u8 resp_flags = 0x80; /* default to failure; cleared on success */
  657. SM_ENTRY(EAP, INITIATE_RECEIVED);
  658. sm->rxInitiate = FALSE;
  659. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_ERP_TYPE_REAUTH,
  660. sm->eap_if.eapRespData, &len);
  661. if (pos == NULL) {
  662. wpa_printf(MSG_INFO, "EAP-Initiate: Invalid frame");
  663. goto fail;
  664. }
  665. hdr = wpabuf_head(sm->eap_if.eapRespData);
  666. ehdr = wpabuf_head(sm->eap_if.eapRespData);
  667. wpa_hexdump(MSG_DEBUG, "EAP: EAP-Initiate/Re-Auth", pos, len);
  668. if (len < 4) {
  669. wpa_printf(MSG_INFO, "EAP: Too short EAP-Initiate/Re-auth");
  670. goto fail;
  671. }
  672. end = pos + len;
  673. flags = *pos++;
  674. seq = WPA_GET_BE16(pos);
  675. pos += 2;
  676. wpa_printf(MSG_DEBUG, "EAP: Flags=0x%x SEQ=%u", flags, seq);
  677. tlvs = pos;
  678. /*
  679. * Parse TVs/TLVs. Since we do not yet know the length of the
  680. * Authentication Tag, stop parsing if an unknown TV/TLV is seen and
  681. * just try to find the keyName-NAI first so that we can check the
  682. * Authentication Tag.
  683. */
  684. if (erp_parse_tlvs(tlvs, end, &parse, 1) < 0)
  685. goto fail;
  686. if (!parse.keyname) {
  687. wpa_printf(MSG_DEBUG,
  688. "EAP: No keyName-NAI in EAP-Initiate/Re-auth Packet");
  689. goto fail;
  690. }
  691. wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Initiate/Re-auth - keyName-NAI",
  692. parse.keyname, parse.keyname_len);
  693. if (parse.keyname_len > 253) {
  694. wpa_printf(MSG_DEBUG,
  695. "EAP: Too long keyName-NAI in EAP-Initiate/Re-auth");
  696. goto fail;
  697. }
  698. os_memcpy(nai, parse.keyname, parse.keyname_len);
  699. nai[parse.keyname_len] = '\0';
  700. if (!sm->eap_server) {
  701. /*
  702. * In passthrough case, EAP-Initiate/Re-auth replaces
  703. * EAP Identity exchange. Use keyName-NAI as the user identity
  704. * and forward EAP-Initiate/Re-auth to the backend
  705. * authentication server.
  706. */
  707. wpa_printf(MSG_DEBUG,
  708. "EAP: Use keyName-NAI as user identity for backend authentication");
  709. eap_server_clear_identity(sm);
  710. sm->identity = (u8 *) dup_binstr(parse.keyname,
  711. parse.keyname_len);
  712. if (!sm->identity)
  713. goto fail;
  714. sm->identity_len = parse.keyname_len;
  715. return;
  716. }
  717. erp = eap_erp_get_key(sm, nai);
  718. if (!erp) {
  719. wpa_printf(MSG_DEBUG, "EAP: No matching ERP key found for %s",
  720. nai);
  721. goto report_error;
  722. }
  723. if (erp->recv_seq != (u32) -1 && erp->recv_seq >= seq) {
  724. wpa_printf(MSG_DEBUG,
  725. "EAP: SEQ=%u replayed (already received SEQ=%u)",
  726. seq, erp->recv_seq);
  727. goto fail;
  728. }
  729. /* Is there enough room for Cryptosuite and Authentication Tag? */
  730. start = parse.keyname + parse.keyname_len;
  731. max_len = end - start;
  732. if (max_len <
  733. 1 + (erp->cryptosuite == EAP_ERP_CS_HMAC_SHA256_256 ? 32 : 16)) {
  734. wpa_printf(MSG_DEBUG,
  735. "EAP: Not enough room for Authentication Tag");
  736. goto fail;
  737. }
  738. switch (erp->cryptosuite) {
  739. case EAP_ERP_CS_HMAC_SHA256_256:
  740. if (end[-33] != erp->cryptosuite) {
  741. wpa_printf(MSG_DEBUG,
  742. "EAP: Different Cryptosuite used");
  743. goto fail;
  744. }
  745. hash_len = 32;
  746. break;
  747. case EAP_ERP_CS_HMAC_SHA256_128:
  748. if (end[-17] != erp->cryptosuite) {
  749. wpa_printf(MSG_DEBUG,
  750. "EAP: Different Cryptosuite used");
  751. goto fail;
  752. }
  753. hash_len = 16;
  754. break;
  755. default:
  756. hash_len = 0;
  757. break;
  758. }
  759. if (hash_len) {
  760. if (hmac_sha256(erp->rIK, erp->rIK_len, hdr,
  761. end - hdr - hash_len, hash) < 0)
  762. goto fail;
  763. if (os_memcmp(end - hash_len, hash, hash_len) != 0) {
  764. wpa_printf(MSG_DEBUG,
  765. "EAP: Authentication Tag mismatch");
  766. goto fail;
  767. }
  768. }
  769. /* Check if any supported CS results in matching tag */
  770. if (!hash_len && max_len >= 1 + 32 &&
  771. end[-33] == EAP_ERP_CS_HMAC_SHA256_256) {
  772. if (hmac_sha256(erp->rIK, erp->rIK_len, hdr,
  773. end - hdr - 32, hash) < 0)
  774. goto fail;
  775. if (os_memcmp(end - 32, hash, 32) == 0) {
  776. wpa_printf(MSG_DEBUG,
  777. "EAP: Authentication Tag match using HMAC-SHA256-256");
  778. hash_len = 32;
  779. erp->cryptosuite = EAP_ERP_CS_HMAC_SHA256_256;
  780. }
  781. }
  782. if (!hash_len && end[-17] == EAP_ERP_CS_HMAC_SHA256_128) {
  783. if (hmac_sha256(erp->rIK, erp->rIK_len, hdr,
  784. end - hdr - 16, hash) < 0)
  785. goto fail;
  786. if (os_memcmp(end - 16, hash, 16) == 0) {
  787. wpa_printf(MSG_DEBUG,
  788. "EAP: Authentication Tag match using HMAC-SHA256-128");
  789. hash_len = 16;
  790. erp->cryptosuite = EAP_ERP_CS_HMAC_SHA256_128;
  791. }
  792. }
  793. if (!hash_len) {
  794. wpa_printf(MSG_DEBUG,
  795. "EAP: No supported cryptosuite matched Authentication Tag");
  796. goto fail;
  797. }
  798. end -= 1 + hash_len;
  799. /*
  800. * Parse TVs/TLVs again now that we know the exact part of the buffer
  801. * that contains them.
  802. */
  803. wpa_hexdump(MSG_DEBUG, "EAP: EAP-Initiate/Re-Auth TVs/TLVs",
  804. tlvs, end - tlvs);
  805. if (erp_parse_tlvs(tlvs, end, &parse, 0) < 0)
  806. goto fail;
  807. wpa_printf(MSG_DEBUG, "EAP: ERP key %s SEQ updated to %u",
  808. erp->keyname_nai, seq);
  809. erp->recv_seq = seq;
  810. resp_flags &= ~0x80; /* R=0 - success */
  811. report_error:
  812. erp_send_finish_reauth(sm, erp, ehdr->identifier, resp_flags, seq, nai);
  813. return;
  814. fail:
  815. sm->ignore = TRUE;
  816. }
  817. #endif /* CONFIG_ERP */
  818. SM_STATE(EAP, INITIALIZE_PASSTHROUGH)
  819. {
  820. SM_ENTRY(EAP, INITIALIZE_PASSTHROUGH);
  821. wpabuf_free(sm->eap_if.aaaEapRespData);
  822. sm->eap_if.aaaEapRespData = NULL;
  823. sm->try_initiate_reauth = FALSE;
  824. }
  825. SM_STATE(EAP, IDLE2)
  826. {
  827. SM_ENTRY(EAP, IDLE2);
  828. sm->eap_if.retransWhile = eap_sm_calculateTimeout(
  829. sm, sm->retransCount, sm->eap_if.eapSRTT, sm->eap_if.eapRTTVAR,
  830. sm->methodTimeout);
  831. }
  832. SM_STATE(EAP, RETRANSMIT2)
  833. {
  834. SM_ENTRY(EAP, RETRANSMIT2);
  835. sm->retransCount++;
  836. if (sm->retransCount <= sm->MaxRetrans && sm->lastReqData) {
  837. if (eap_copy_buf(&sm->eap_if.eapReqData, sm->lastReqData) == 0)
  838. sm->eap_if.eapReq = TRUE;
  839. }
  840. }
  841. SM_STATE(EAP, RECEIVED2)
  842. {
  843. SM_ENTRY(EAP, RECEIVED2);
  844. /* parse rxResp, respId, respMethod */
  845. eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
  846. }
  847. SM_STATE(EAP, DISCARD2)
  848. {
  849. SM_ENTRY(EAP, DISCARD2);
  850. sm->eap_if.eapResp = FALSE;
  851. sm->eap_if.eapNoReq = TRUE;
  852. }
  853. SM_STATE(EAP, SEND_REQUEST2)
  854. {
  855. SM_ENTRY(EAP, SEND_REQUEST2);
  856. sm->retransCount = 0;
  857. if (sm->eap_if.eapReqData) {
  858. if (eap_copy_buf(&sm->lastReqData, sm->eap_if.eapReqData) == 0)
  859. {
  860. sm->eap_if.eapResp = FALSE;
  861. sm->eap_if.eapReq = TRUE;
  862. } else {
  863. sm->eap_if.eapResp = FALSE;
  864. sm->eap_if.eapReq = FALSE;
  865. }
  866. } else {
  867. wpa_printf(MSG_INFO, "EAP: SEND_REQUEST2 - no eapReqData");
  868. sm->eap_if.eapResp = FALSE;
  869. sm->eap_if.eapReq = FALSE;
  870. sm->eap_if.eapNoReq = TRUE;
  871. }
  872. }
  873. SM_STATE(EAP, AAA_REQUEST)
  874. {
  875. SM_ENTRY(EAP, AAA_REQUEST);
  876. if (sm->eap_if.eapRespData == NULL) {
  877. wpa_printf(MSG_INFO, "EAP: AAA_REQUEST - no eapRespData");
  878. return;
  879. }
  880. /*
  881. * if (respMethod == IDENTITY)
  882. * aaaIdentity = eapRespData
  883. * This is already taken care of by the EAP-Identity method which
  884. * stores the identity into sm->identity.
  885. */
  886. eap_copy_buf(&sm->eap_if.aaaEapRespData, sm->eap_if.eapRespData);
  887. }
  888. SM_STATE(EAP, AAA_RESPONSE)
  889. {
  890. SM_ENTRY(EAP, AAA_RESPONSE);
  891. eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
  892. sm->currentId = eap_sm_getId(sm->eap_if.eapReqData);
  893. sm->methodTimeout = sm->eap_if.aaaMethodTimeout;
  894. }
  895. SM_STATE(EAP, AAA_IDLE)
  896. {
  897. SM_ENTRY(EAP, AAA_IDLE);
  898. sm->eap_if.aaaFail = FALSE;
  899. sm->eap_if.aaaSuccess = FALSE;
  900. sm->eap_if.aaaEapReq = FALSE;
  901. sm->eap_if.aaaEapNoReq = FALSE;
  902. sm->eap_if.aaaEapResp = TRUE;
  903. }
  904. SM_STATE(EAP, TIMEOUT_FAILURE2)
  905. {
  906. SM_ENTRY(EAP, TIMEOUT_FAILURE2);
  907. sm->eap_if.eapTimeout = TRUE;
  908. }
  909. SM_STATE(EAP, FAILURE2)
  910. {
  911. SM_ENTRY(EAP, FAILURE2);
  912. eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
  913. sm->eap_if.eapFail = TRUE;
  914. }
  915. SM_STATE(EAP, SUCCESS2)
  916. {
  917. SM_ENTRY(EAP, SUCCESS2);
  918. eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
  919. sm->eap_if.eapKeyAvailable = sm->eap_if.aaaEapKeyAvailable;
  920. if (sm->eap_if.aaaEapKeyAvailable) {
  921. EAP_COPY(&sm->eap_if.eapKeyData, sm->eap_if.aaaEapKeyData);
  922. } else {
  923. bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
  924. sm->eap_if.eapKeyData = NULL;
  925. sm->eap_if.eapKeyDataLen = 0;
  926. }
  927. sm->eap_if.eapSuccess = TRUE;
  928. /*
  929. * Start reauthentication with identity request even though we know the
  930. * previously used identity. This is needed to get reauthentication
  931. * started properly.
  932. */
  933. sm->start_reauth = TRUE;
  934. }
  935. SM_STEP(EAP)
  936. {
  937. if (sm->eap_if.eapRestart && sm->eap_if.portEnabled)
  938. SM_ENTER_GLOBAL(EAP, INITIALIZE);
  939. else if (!sm->eap_if.portEnabled)
  940. SM_ENTER_GLOBAL(EAP, DISABLED);
  941. else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
  942. if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
  943. wpa_printf(MSG_DEBUG, "EAP: more than %d "
  944. "authentication rounds - abort",
  945. EAP_MAX_AUTH_ROUNDS);
  946. sm->num_rounds++;
  947. SM_ENTER_GLOBAL(EAP, FAILURE);
  948. }
  949. } else switch (sm->EAP_state) {
  950. case EAP_INITIALIZE:
  951. if (sm->backend_auth) {
  952. if (!sm->rxResp)
  953. SM_ENTER(EAP, SELECT_ACTION);
  954. else if (sm->rxResp &&
  955. (sm->respMethod == EAP_TYPE_NAK ||
  956. (sm->respMethod == EAP_TYPE_EXPANDED &&
  957. sm->respVendor == EAP_VENDOR_IETF &&
  958. sm->respVendorMethod == EAP_TYPE_NAK)))
  959. SM_ENTER(EAP, NAK);
  960. else
  961. SM_ENTER(EAP, PICK_UP_METHOD);
  962. } else {
  963. SM_ENTER(EAP, SELECT_ACTION);
  964. }
  965. break;
  966. case EAP_PICK_UP_METHOD:
  967. if (sm->currentMethod == EAP_TYPE_NONE) {
  968. SM_ENTER(EAP, SELECT_ACTION);
  969. } else {
  970. SM_ENTER(EAP, METHOD_RESPONSE);
  971. }
  972. break;
  973. case EAP_DISABLED:
  974. if (sm->eap_if.portEnabled)
  975. SM_ENTER(EAP, INITIALIZE);
  976. break;
  977. case EAP_IDLE:
  978. if (sm->eap_if.retransWhile == 0) {
  979. if (sm->try_initiate_reauth) {
  980. sm->try_initiate_reauth = FALSE;
  981. SM_ENTER(EAP, SELECT_ACTION);
  982. } else {
  983. SM_ENTER(EAP, RETRANSMIT);
  984. }
  985. } else if (sm->eap_if.eapResp)
  986. SM_ENTER(EAP, RECEIVED);
  987. break;
  988. case EAP_RETRANSMIT:
  989. if (sm->retransCount > sm->MaxRetrans)
  990. SM_ENTER(EAP, TIMEOUT_FAILURE);
  991. else
  992. SM_ENTER(EAP, IDLE);
  993. break;
  994. case EAP_RECEIVED:
  995. if (sm->rxResp && (sm->respId == sm->currentId) &&
  996. (sm->respMethod == EAP_TYPE_NAK ||
  997. (sm->respMethod == EAP_TYPE_EXPANDED &&
  998. sm->respVendor == EAP_VENDOR_IETF &&
  999. sm->respVendorMethod == EAP_TYPE_NAK))
  1000. && (sm->methodState == METHOD_PROPOSED))
  1001. SM_ENTER(EAP, NAK);
  1002. else if (sm->rxResp && (sm->respId == sm->currentId) &&
  1003. ((sm->respMethod == sm->currentMethod) ||
  1004. (sm->respMethod == EAP_TYPE_EXPANDED &&
  1005. sm->respVendor == EAP_VENDOR_IETF &&
  1006. sm->respVendorMethod == sm->currentMethod)))
  1007. SM_ENTER(EAP, INTEGRITY_CHECK);
  1008. #ifdef CONFIG_ERP
  1009. else if (sm->rxInitiate)
  1010. SM_ENTER(EAP, INITIATE_RECEIVED);
  1011. #endif /* CONFIG_ERP */
  1012. else {
  1013. wpa_printf(MSG_DEBUG, "EAP: RECEIVED->DISCARD: "
  1014. "rxResp=%d respId=%d currentId=%d "
  1015. "respMethod=%d currentMethod=%d",
  1016. sm->rxResp, sm->respId, sm->currentId,
  1017. sm->respMethod, sm->currentMethod);
  1018. eap_log_msg(sm, "Discard received EAP message");
  1019. SM_ENTER(EAP, DISCARD);
  1020. }
  1021. break;
  1022. case EAP_DISCARD:
  1023. SM_ENTER(EAP, IDLE);
  1024. break;
  1025. case EAP_SEND_REQUEST:
  1026. SM_ENTER(EAP, IDLE);
  1027. break;
  1028. case EAP_INTEGRITY_CHECK:
  1029. if (sm->ignore)
  1030. SM_ENTER(EAP, DISCARD);
  1031. else
  1032. SM_ENTER(EAP, METHOD_RESPONSE);
  1033. break;
  1034. case EAP_METHOD_REQUEST:
  1035. if (sm->m == NULL) {
  1036. /*
  1037. * This transition is not mentioned in RFC 4137, but it
  1038. * is needed to handle cleanly a case where EAP method
  1039. * initialization fails.
  1040. */
  1041. SM_ENTER(EAP, FAILURE);
  1042. break;
  1043. }
  1044. SM_ENTER(EAP, SEND_REQUEST);
  1045. break;
  1046. case EAP_METHOD_RESPONSE:
  1047. /*
  1048. * Note: Mechanism to allow EAP methods to wait while going
  1049. * through pending processing is an extension to RFC 4137
  1050. * which only defines the transits to SELECT_ACTION and
  1051. * METHOD_REQUEST from this METHOD_RESPONSE state.
  1052. */
  1053. if (sm->methodState == METHOD_END)
  1054. SM_ENTER(EAP, SELECT_ACTION);
  1055. else if (sm->method_pending == METHOD_PENDING_WAIT) {
  1056. wpa_printf(MSG_DEBUG, "EAP: Method has pending "
  1057. "processing - wait before proceeding to "
  1058. "METHOD_REQUEST state");
  1059. } else if (sm->method_pending == METHOD_PENDING_CONT) {
  1060. wpa_printf(MSG_DEBUG, "EAP: Method has completed "
  1061. "pending processing - reprocess pending "
  1062. "EAP message");
  1063. sm->method_pending = METHOD_PENDING_NONE;
  1064. SM_ENTER(EAP, METHOD_RESPONSE);
  1065. } else
  1066. SM_ENTER(EAP, METHOD_REQUEST);
  1067. break;
  1068. case EAP_PROPOSE_METHOD:
  1069. /*
  1070. * Note: Mechanism to allow EAP methods to wait while going
  1071. * through pending processing is an extension to RFC 4137
  1072. * which only defines the transit to METHOD_REQUEST from this
  1073. * PROPOSE_METHOD state.
  1074. */
  1075. if (sm->method_pending == METHOD_PENDING_WAIT) {
  1076. wpa_printf(MSG_DEBUG, "EAP: Method has pending "
  1077. "processing - wait before proceeding to "
  1078. "METHOD_REQUEST state");
  1079. if (sm->user_eap_method_index > 0)
  1080. sm->user_eap_method_index--;
  1081. } else if (sm->method_pending == METHOD_PENDING_CONT) {
  1082. wpa_printf(MSG_DEBUG, "EAP: Method has completed "
  1083. "pending processing - reprocess pending "
  1084. "EAP message");
  1085. sm->method_pending = METHOD_PENDING_NONE;
  1086. SM_ENTER(EAP, PROPOSE_METHOD);
  1087. } else
  1088. SM_ENTER(EAP, METHOD_REQUEST);
  1089. break;
  1090. case EAP_NAK:
  1091. SM_ENTER(EAP, SELECT_ACTION);
  1092. break;
  1093. case EAP_SELECT_ACTION:
  1094. if (sm->decision == DECISION_FAILURE)
  1095. SM_ENTER(EAP, FAILURE);
  1096. else if (sm->decision == DECISION_SUCCESS)
  1097. SM_ENTER(EAP, SUCCESS);
  1098. else if (sm->decision == DECISION_PASSTHROUGH)
  1099. SM_ENTER(EAP, INITIALIZE_PASSTHROUGH);
  1100. else if (sm->decision == DECISION_INITIATE_REAUTH_START)
  1101. SM_ENTER(EAP, INITIATE_REAUTH_START);
  1102. #ifdef CONFIG_ERP
  1103. else if (sm->eap_server && sm->erp && sm->rxInitiate)
  1104. SM_ENTER(EAP, INITIATE_RECEIVED);
  1105. #endif /* CONFIG_ERP */
  1106. else
  1107. SM_ENTER(EAP, PROPOSE_METHOD);
  1108. break;
  1109. case EAP_INITIATE_REAUTH_START:
  1110. SM_ENTER(EAP, SEND_REQUEST);
  1111. break;
  1112. case EAP_INITIATE_RECEIVED:
  1113. if (!sm->eap_server)
  1114. SM_ENTER(EAP, SELECT_ACTION);
  1115. break;
  1116. case EAP_TIMEOUT_FAILURE:
  1117. break;
  1118. case EAP_FAILURE:
  1119. break;
  1120. case EAP_SUCCESS:
  1121. break;
  1122. case EAP_INITIALIZE_PASSTHROUGH:
  1123. if (sm->currentId == -1)
  1124. SM_ENTER(EAP, AAA_IDLE);
  1125. else
  1126. SM_ENTER(EAP, AAA_REQUEST);
  1127. break;
  1128. case EAP_IDLE2:
  1129. if (sm->eap_if.eapResp)
  1130. SM_ENTER(EAP, RECEIVED2);
  1131. else if (sm->eap_if.retransWhile == 0)
  1132. SM_ENTER(EAP, RETRANSMIT2);
  1133. break;
  1134. case EAP_RETRANSMIT2:
  1135. if (sm->retransCount > sm->MaxRetrans)
  1136. SM_ENTER(EAP, TIMEOUT_FAILURE2);
  1137. else
  1138. SM_ENTER(EAP, IDLE2);
  1139. break;
  1140. case EAP_RECEIVED2:
  1141. if (sm->rxResp && (sm->respId == sm->currentId))
  1142. SM_ENTER(EAP, AAA_REQUEST);
  1143. else
  1144. SM_ENTER(EAP, DISCARD2);
  1145. break;
  1146. case EAP_DISCARD2:
  1147. SM_ENTER(EAP, IDLE2);
  1148. break;
  1149. case EAP_SEND_REQUEST2:
  1150. SM_ENTER(EAP, IDLE2);
  1151. break;
  1152. case EAP_AAA_REQUEST:
  1153. SM_ENTER(EAP, AAA_IDLE);
  1154. break;
  1155. case EAP_AAA_RESPONSE:
  1156. SM_ENTER(EAP, SEND_REQUEST2);
  1157. break;
  1158. case EAP_AAA_IDLE:
  1159. if (sm->eap_if.aaaFail)
  1160. SM_ENTER(EAP, FAILURE2);
  1161. else if (sm->eap_if.aaaSuccess)
  1162. SM_ENTER(EAP, SUCCESS2);
  1163. else if (sm->eap_if.aaaEapReq)
  1164. SM_ENTER(EAP, AAA_RESPONSE);
  1165. else if (sm->eap_if.aaaTimeout)
  1166. SM_ENTER(EAP, TIMEOUT_FAILURE2);
  1167. break;
  1168. case EAP_TIMEOUT_FAILURE2:
  1169. break;
  1170. case EAP_FAILURE2:
  1171. break;
  1172. case EAP_SUCCESS2:
  1173. break;
  1174. }
  1175. }
  1176. static int eap_sm_calculateTimeout(struct eap_sm *sm, int retransCount,
  1177. int eapSRTT, int eapRTTVAR,
  1178. int methodTimeout)
  1179. {
  1180. int rto, i;
  1181. if (sm->try_initiate_reauth) {
  1182. wpa_printf(MSG_DEBUG,
  1183. "EAP: retransmit timeout 1 second for EAP-Initiate-Re-auth-Start");
  1184. return 1;
  1185. }
  1186. if (methodTimeout) {
  1187. /*
  1188. * EAP method (either internal or through AAA server, provided
  1189. * timeout hint. Use that as-is as a timeout for retransmitting
  1190. * the EAP request if no response is received.
  1191. */
  1192. wpa_printf(MSG_DEBUG, "EAP: retransmit timeout %d seconds "
  1193. "(from EAP method hint)", methodTimeout);
  1194. return methodTimeout;
  1195. }
  1196. /*
  1197. * RFC 3748 recommends algorithms described in RFC 2988 for estimation
  1198. * of the retransmission timeout. This should be implemented once
  1199. * round-trip time measurements are available. For nowm a simple
  1200. * backoff mechanism is used instead if there are no EAP method
  1201. * specific hints.
  1202. *
  1203. * SRTT = smoothed round-trip time
  1204. * RTTVAR = round-trip time variation
  1205. * RTO = retransmission timeout
  1206. */
  1207. /*
  1208. * RFC 2988, 2.1: before RTT measurement, set RTO to 3 seconds for
  1209. * initial retransmission and then double the RTO to provide back off
  1210. * per 5.5. Limit the maximum RTO to 20 seconds per RFC 3748, 4.3
  1211. * modified RTOmax.
  1212. */
  1213. rto = 3;
  1214. for (i = 0; i < retransCount; i++) {
  1215. rto *= 2;
  1216. if (rto >= 20) {
  1217. rto = 20;
  1218. break;
  1219. }
  1220. }
  1221. wpa_printf(MSG_DEBUG, "EAP: retransmit timeout %d seconds "
  1222. "(from dynamic back off; retransCount=%d)",
  1223. rto, retransCount);
  1224. return rto;
  1225. }
  1226. static void eap_sm_parseEapResp(struct eap_sm *sm, const struct wpabuf *resp)
  1227. {
  1228. const struct eap_hdr *hdr;
  1229. size_t plen;
  1230. /* parse rxResp, respId, respMethod */
  1231. sm->rxResp = FALSE;
  1232. sm->rxInitiate = FALSE;
  1233. sm->respId = -1;
  1234. sm->respMethod = EAP_TYPE_NONE;
  1235. sm->respVendor = EAP_VENDOR_IETF;
  1236. sm->respVendorMethod = EAP_TYPE_NONE;
  1237. if (resp == NULL || wpabuf_len(resp) < sizeof(*hdr)) {
  1238. wpa_printf(MSG_DEBUG, "EAP: parseEapResp: invalid resp=%p "
  1239. "len=%lu", resp,
  1240. resp ? (unsigned long) wpabuf_len(resp) : 0);
  1241. return;
  1242. }
  1243. hdr = wpabuf_head(resp);
  1244. plen = be_to_host16(hdr->length);
  1245. if (plen > wpabuf_len(resp)) {
  1246. wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
  1247. "(len=%lu plen=%lu)",
  1248. (unsigned long) wpabuf_len(resp),
  1249. (unsigned long) plen);
  1250. return;
  1251. }
  1252. sm->respId = hdr->identifier;
  1253. if (hdr->code == EAP_CODE_RESPONSE)
  1254. sm->rxResp = TRUE;
  1255. else if (hdr->code == EAP_CODE_INITIATE)
  1256. sm->rxInitiate = TRUE;
  1257. if (plen > sizeof(*hdr)) {
  1258. u8 *pos = (u8 *) (hdr + 1);
  1259. sm->respMethod = *pos++;
  1260. if (sm->respMethod == EAP_TYPE_EXPANDED) {
  1261. if (plen < sizeof(*hdr) + 8) {
  1262. wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
  1263. "expanded EAP-Packet (plen=%lu)",
  1264. (unsigned long) plen);
  1265. return;
  1266. }
  1267. sm->respVendor = WPA_GET_BE24(pos);
  1268. pos += 3;
  1269. sm->respVendorMethod = WPA_GET_BE32(pos);
  1270. }
  1271. }
  1272. wpa_printf(MSG_DEBUG,
  1273. "EAP: parseEapResp: rxResp=%d rxInitiate=%d respId=%d respMethod=%u respVendor=%u respVendorMethod=%u",
  1274. sm->rxResp, sm->rxInitiate, sm->respId, sm->respMethod,
  1275. sm->respVendor, sm->respVendorMethod);
  1276. }
  1277. static int eap_sm_getId(const struct wpabuf *data)
  1278. {
  1279. const struct eap_hdr *hdr;
  1280. if (data == NULL || wpabuf_len(data) < sizeof(*hdr))
  1281. return -1;
  1282. hdr = wpabuf_head(data);
  1283. wpa_printf(MSG_DEBUG, "EAP: getId: id=%d", hdr->identifier);
  1284. return hdr->identifier;
  1285. }
  1286. static struct wpabuf * eap_sm_buildSuccess(struct eap_sm *sm, u8 id)
  1287. {
  1288. struct wpabuf *msg;
  1289. struct eap_hdr *resp;
  1290. wpa_printf(MSG_DEBUG, "EAP: Building EAP-Success (id=%d)", id);
  1291. msg = wpabuf_alloc(sizeof(*resp));
  1292. if (msg == NULL)
  1293. return NULL;
  1294. resp = wpabuf_put(msg, sizeof(*resp));
  1295. resp->code = EAP_CODE_SUCCESS;
  1296. resp->identifier = id;
  1297. resp->length = host_to_be16(sizeof(*resp));
  1298. return msg;
  1299. }
  1300. static struct wpabuf * eap_sm_buildFailure(struct eap_sm *sm, u8 id)
  1301. {
  1302. struct wpabuf *msg;
  1303. struct eap_hdr *resp;
  1304. wpa_printf(MSG_DEBUG, "EAP: Building EAP-Failure (id=%d)", id);
  1305. msg = wpabuf_alloc(sizeof(*resp));
  1306. if (msg == NULL)
  1307. return NULL;
  1308. resp = wpabuf_put(msg, sizeof(*resp));
  1309. resp->code = EAP_CODE_FAILURE;
  1310. resp->identifier = id;
  1311. resp->length = host_to_be16(sizeof(*resp));
  1312. return msg;
  1313. }
  1314. static int eap_sm_nextId(struct eap_sm *sm, int id)
  1315. {
  1316. if (id < 0) {
  1317. /* RFC 3748 Ch 4.1: recommended to initialize Identifier with a
  1318. * random number */
  1319. id = rand() & 0xff;
  1320. if (id != sm->lastId)
  1321. return id;
  1322. }
  1323. return (id + 1) & 0xff;
  1324. }
  1325. /**
  1326. * eap_sm_process_nak - Process EAP-Response/Nak
  1327. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1328. * @nak_list: Nak list (allowed methods) from the supplicant
  1329. * @len: Length of nak_list in bytes
  1330. *
  1331. * This function is called when EAP-Response/Nak is received from the
  1332. * supplicant. This can happen for both phase 1 and phase 2 authentications.
  1333. */
  1334. void eap_sm_process_nak(struct eap_sm *sm, const u8 *nak_list, size_t len)
  1335. {
  1336. int i;
  1337. size_t j;
  1338. if (sm->user == NULL)
  1339. return;
  1340. wpa_printf(MSG_MSGDUMP, "EAP: processing NAK (current EAP method "
  1341. "index %d)", sm->user_eap_method_index);
  1342. wpa_hexdump(MSG_MSGDUMP, "EAP: configured methods",
  1343. (u8 *) sm->user->methods,
  1344. EAP_MAX_METHODS * sizeof(sm->user->methods[0]));
  1345. wpa_hexdump(MSG_MSGDUMP, "EAP: list of methods supported by the peer",
  1346. nak_list, len);
  1347. i = sm->user_eap_method_index;
  1348. while (i < EAP_MAX_METHODS &&
  1349. (sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
  1350. sm->user->methods[i].method != EAP_TYPE_NONE)) {
  1351. if (sm->user->methods[i].vendor != EAP_VENDOR_IETF)
  1352. goto not_found;
  1353. for (j = 0; j < len; j++) {
  1354. if (nak_list[j] == sm->user->methods[i].method) {
  1355. break;
  1356. }
  1357. }
  1358. if (j < len) {
  1359. /* found */
  1360. i++;
  1361. continue;
  1362. }
  1363. not_found:
  1364. /* not found - remove from the list */
  1365. if (i + 1 < EAP_MAX_METHODS) {
  1366. os_memmove(&sm->user->methods[i],
  1367. &sm->user->methods[i + 1],
  1368. (EAP_MAX_METHODS - i - 1) *
  1369. sizeof(sm->user->methods[0]));
  1370. }
  1371. sm->user->methods[EAP_MAX_METHODS - 1].vendor =
  1372. EAP_VENDOR_IETF;
  1373. sm->user->methods[EAP_MAX_METHODS - 1].method = EAP_TYPE_NONE;
  1374. }
  1375. wpa_hexdump(MSG_MSGDUMP, "EAP: new list of configured methods",
  1376. (u8 *) sm->user->methods, EAP_MAX_METHODS *
  1377. sizeof(sm->user->methods[0]));
  1378. }
  1379. static void eap_sm_Policy_update(struct eap_sm *sm, const u8 *nak_list,
  1380. size_t len)
  1381. {
  1382. if (nak_list == NULL || sm == NULL || sm->user == NULL)
  1383. return;
  1384. if (sm->user->phase2) {
  1385. wpa_printf(MSG_DEBUG, "EAP: EAP-Nak received after Phase2 user"
  1386. " info was selected - reject");
  1387. sm->decision = DECISION_FAILURE;
  1388. return;
  1389. }
  1390. eap_sm_process_nak(sm, nak_list, len);
  1391. }
  1392. static EapType eap_sm_Policy_getNextMethod(struct eap_sm *sm, int *vendor)
  1393. {
  1394. EapType next;
  1395. int idx = sm->user_eap_method_index;
  1396. /* In theory, there should be no problems with starting
  1397. * re-authentication with something else than EAP-Request/Identity and
  1398. * this does indeed work with wpa_supplicant. However, at least Funk
  1399. * Supplicant seemed to ignore re-auth if it skipped
  1400. * EAP-Request/Identity.
  1401. * Re-auth sets currentId == -1, so that can be used here to select
  1402. * whether Identity needs to be requested again. */
  1403. if (sm->identity == NULL || sm->currentId == -1) {
  1404. *vendor = EAP_VENDOR_IETF;
  1405. next = EAP_TYPE_IDENTITY;
  1406. sm->update_user = TRUE;
  1407. } else if (sm->user && idx < EAP_MAX_METHODS &&
  1408. (sm->user->methods[idx].vendor != EAP_VENDOR_IETF ||
  1409. sm->user->methods[idx].method != EAP_TYPE_NONE)) {
  1410. *vendor = sm->user->methods[idx].vendor;
  1411. next = sm->user->methods[idx].method;
  1412. sm->user_eap_method_index++;
  1413. } else {
  1414. *vendor = EAP_VENDOR_IETF;
  1415. next = EAP_TYPE_NONE;
  1416. }
  1417. wpa_printf(MSG_DEBUG, "EAP: getNextMethod: vendor %d type %d",
  1418. *vendor, next);
  1419. return next;
  1420. }
  1421. static int eap_sm_Policy_getDecision(struct eap_sm *sm)
  1422. {
  1423. if (!sm->eap_server && sm->identity && !sm->start_reauth) {
  1424. wpa_printf(MSG_DEBUG, "EAP: getDecision: -> PASSTHROUGH");
  1425. return DECISION_PASSTHROUGH;
  1426. }
  1427. if (sm->m && sm->currentMethod != EAP_TYPE_IDENTITY &&
  1428. sm->m->isSuccess(sm, sm->eap_method_priv)) {
  1429. wpa_printf(MSG_DEBUG, "EAP: getDecision: method succeeded -> "
  1430. "SUCCESS");
  1431. sm->update_user = TRUE;
  1432. return DECISION_SUCCESS;
  1433. }
  1434. if (sm->m && sm->m->isDone(sm, sm->eap_method_priv) &&
  1435. !sm->m->isSuccess(sm, sm->eap_method_priv)) {
  1436. wpa_printf(MSG_DEBUG, "EAP: getDecision: method failed -> "
  1437. "FAILURE");
  1438. sm->update_user = TRUE;
  1439. return DECISION_FAILURE;
  1440. }
  1441. if ((sm->user == NULL || sm->update_user) && sm->identity &&
  1442. !sm->start_reauth) {
  1443. /*
  1444. * Allow Identity method to be started once to allow identity
  1445. * selection hint to be sent from the authentication server,
  1446. * but prevent a loop of Identity requests by only allowing
  1447. * this to happen once.
  1448. */
  1449. int id_req = 0;
  1450. if (sm->user && sm->currentMethod == EAP_TYPE_IDENTITY &&
  1451. sm->user->methods[0].vendor == EAP_VENDOR_IETF &&
  1452. sm->user->methods[0].method == EAP_TYPE_IDENTITY)
  1453. id_req = 1;
  1454. if (eap_user_get(sm, sm->identity, sm->identity_len, 0) != 0) {
  1455. wpa_printf(MSG_DEBUG, "EAP: getDecision: user not "
  1456. "found from database -> FAILURE");
  1457. return DECISION_FAILURE;
  1458. }
  1459. if (id_req && sm->user &&
  1460. sm->user->methods[0].vendor == EAP_VENDOR_IETF &&
  1461. sm->user->methods[0].method == EAP_TYPE_IDENTITY) {
  1462. wpa_printf(MSG_DEBUG, "EAP: getDecision: stop "
  1463. "identity request loop -> FAILURE");
  1464. sm->update_user = TRUE;
  1465. return DECISION_FAILURE;
  1466. }
  1467. sm->update_user = FALSE;
  1468. }
  1469. sm->start_reauth = FALSE;
  1470. if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
  1471. (sm->user->methods[sm->user_eap_method_index].vendor !=
  1472. EAP_VENDOR_IETF ||
  1473. sm->user->methods[sm->user_eap_method_index].method !=
  1474. EAP_TYPE_NONE)) {
  1475. wpa_printf(MSG_DEBUG, "EAP: getDecision: another method "
  1476. "available -> CONTINUE");
  1477. return DECISION_CONTINUE;
  1478. }
  1479. if (!sm->identity && eap_get_erp_send_reauth_start(sm) &&
  1480. !sm->initiate_reauth_start_sent) {
  1481. wpa_printf(MSG_DEBUG,
  1482. "EAP: getDecision: send EAP-Initiate/Re-auth-Start");
  1483. return DECISION_INITIATE_REAUTH_START;
  1484. }
  1485. if (sm->identity == NULL || sm->currentId == -1) {
  1486. wpa_printf(MSG_DEBUG, "EAP: getDecision: no identity known "
  1487. "yet -> CONTINUE");
  1488. return DECISION_CONTINUE;
  1489. }
  1490. wpa_printf(MSG_DEBUG, "EAP: getDecision: no more methods available -> "
  1491. "FAILURE");
  1492. return DECISION_FAILURE;
  1493. }
  1494. static Boolean eap_sm_Policy_doPickUp(struct eap_sm *sm, EapType method)
  1495. {
  1496. return method == EAP_TYPE_IDENTITY ? TRUE : FALSE;
  1497. }
  1498. /**
  1499. * eap_server_sm_step - Step EAP server state machine
  1500. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1501. * Returns: 1 if EAP state was changed or 0 if not
  1502. *
  1503. * This function advances EAP state machine to a new state to match with the
  1504. * current variables. This should be called whenever variables used by the EAP
  1505. * state machine have changed.
  1506. */
  1507. int eap_server_sm_step(struct eap_sm *sm)
  1508. {
  1509. int res = 0;
  1510. do {
  1511. sm->changed = FALSE;
  1512. SM_STEP_RUN(EAP);
  1513. if (sm->changed)
  1514. res = 1;
  1515. } while (sm->changed);
  1516. return res;
  1517. }
  1518. static void eap_user_free(struct eap_user *user)
  1519. {
  1520. if (user == NULL)
  1521. return;
  1522. bin_clear_free(user->password, user->password_len);
  1523. user->password = NULL;
  1524. os_free(user);
  1525. }
  1526. /**
  1527. * eap_server_sm_init - Allocate and initialize EAP server state machine
  1528. * @eapol_ctx: Context data to be used with eapol_cb calls
  1529. * @eapol_cb: Pointer to EAPOL callback functions
  1530. * @conf: EAP configuration
  1531. * Returns: Pointer to the allocated EAP state machine or %NULL on failure
  1532. *
  1533. * This function allocates and initializes an EAP state machine.
  1534. */
  1535. struct eap_sm * eap_server_sm_init(void *eapol_ctx,
  1536. struct eapol_callbacks *eapol_cb,
  1537. struct eap_config *conf)
  1538. {
  1539. struct eap_sm *sm;
  1540. sm = os_zalloc(sizeof(*sm));
  1541. if (sm == NULL)
  1542. return NULL;
  1543. sm->eapol_ctx = eapol_ctx;
  1544. sm->eapol_cb = eapol_cb;
  1545. sm->MaxRetrans = 5; /* RFC 3748: max 3-5 retransmissions suggested */
  1546. sm->ssl_ctx = conf->ssl_ctx;
  1547. sm->msg_ctx = conf->msg_ctx;
  1548. sm->eap_sim_db_priv = conf->eap_sim_db_priv;
  1549. sm->backend_auth = conf->backend_auth;
  1550. sm->eap_server = conf->eap_server;
  1551. if (conf->pac_opaque_encr_key) {
  1552. sm->pac_opaque_encr_key = os_malloc(16);
  1553. if (sm->pac_opaque_encr_key) {
  1554. os_memcpy(sm->pac_opaque_encr_key,
  1555. conf->pac_opaque_encr_key, 16);
  1556. }
  1557. }
  1558. if (conf->eap_fast_a_id) {
  1559. sm->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
  1560. if (sm->eap_fast_a_id) {
  1561. os_memcpy(sm->eap_fast_a_id, conf->eap_fast_a_id,
  1562. conf->eap_fast_a_id_len);
  1563. sm->eap_fast_a_id_len = conf->eap_fast_a_id_len;
  1564. }
  1565. }
  1566. if (conf->eap_fast_a_id_info)
  1567. sm->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
  1568. sm->eap_fast_prov = conf->eap_fast_prov;
  1569. sm->pac_key_lifetime = conf->pac_key_lifetime;
  1570. sm->pac_key_refresh_time = conf->pac_key_refresh_time;
  1571. sm->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
  1572. sm->tnc = conf->tnc;
  1573. sm->wps = conf->wps;
  1574. if (conf->assoc_wps_ie)
  1575. sm->assoc_wps_ie = wpabuf_dup(conf->assoc_wps_ie);
  1576. if (conf->assoc_p2p_ie)
  1577. sm->assoc_p2p_ie = wpabuf_dup(conf->assoc_p2p_ie);
  1578. if (conf->peer_addr)
  1579. os_memcpy(sm->peer_addr, conf->peer_addr, ETH_ALEN);
  1580. sm->fragment_size = conf->fragment_size;
  1581. sm->pwd_group = conf->pwd_group;
  1582. sm->pbc_in_m1 = conf->pbc_in_m1;
  1583. sm->server_id = conf->server_id;
  1584. sm->server_id_len = conf->server_id_len;
  1585. sm->erp = conf->erp;
  1586. #ifdef CONFIG_TESTING_OPTIONS
  1587. sm->tls_test_flags = conf->tls_test_flags;
  1588. #endif /* CONFIG_TESTING_OPTIONS */
  1589. wpa_printf(MSG_DEBUG, "EAP: Server state machine created");
  1590. return sm;
  1591. }
  1592. /**
  1593. * eap_server_sm_deinit - Deinitialize and free an EAP server state machine
  1594. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1595. *
  1596. * This function deinitializes EAP state machine and frees all allocated
  1597. * resources.
  1598. */
  1599. void eap_server_sm_deinit(struct eap_sm *sm)
  1600. {
  1601. if (sm == NULL)
  1602. return;
  1603. wpa_printf(MSG_DEBUG, "EAP: Server state machine removed");
  1604. if (sm->m && sm->eap_method_priv)
  1605. sm->m->reset(sm, sm->eap_method_priv);
  1606. wpabuf_free(sm->eap_if.eapReqData);
  1607. bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
  1608. os_free(sm->eap_if.eapSessionId);
  1609. wpabuf_free(sm->lastReqData);
  1610. wpabuf_free(sm->eap_if.eapRespData);
  1611. os_free(sm->identity);
  1612. os_free(sm->pac_opaque_encr_key);
  1613. os_free(sm->eap_fast_a_id);
  1614. os_free(sm->eap_fast_a_id_info);
  1615. wpabuf_free(sm->eap_if.aaaEapReqData);
  1616. wpabuf_free(sm->eap_if.aaaEapRespData);
  1617. bin_clear_free(sm->eap_if.aaaEapKeyData, sm->eap_if.aaaEapKeyDataLen);
  1618. eap_user_free(sm->user);
  1619. wpabuf_free(sm->assoc_wps_ie);
  1620. wpabuf_free(sm->assoc_p2p_ie);
  1621. os_free(sm);
  1622. }
  1623. /**
  1624. * eap_sm_notify_cached - Notify EAP state machine of cached PMK
  1625. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1626. *
  1627. * This function is called when PMKSA caching is used to skip EAP
  1628. * authentication.
  1629. */
  1630. void eap_sm_notify_cached(struct eap_sm *sm)
  1631. {
  1632. if (sm == NULL)
  1633. return;
  1634. sm->EAP_state = EAP_SUCCESS;
  1635. }
  1636. /**
  1637. * eap_sm_pending_cb - EAP state machine callback for a pending EAP request
  1638. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1639. *
  1640. * This function is called when data for a pending EAP-Request is received.
  1641. */
  1642. void eap_sm_pending_cb(struct eap_sm *sm)
  1643. {
  1644. if (sm == NULL)
  1645. return;
  1646. wpa_printf(MSG_DEBUG, "EAP: Callback for pending request received");
  1647. if (sm->method_pending == METHOD_PENDING_WAIT)
  1648. sm->method_pending = METHOD_PENDING_CONT;
  1649. }
  1650. /**
  1651. * eap_sm_method_pending - Query whether EAP method is waiting for pending data
  1652. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1653. * Returns: 1 if method is waiting for pending data or 0 if not
  1654. */
  1655. int eap_sm_method_pending(struct eap_sm *sm)
  1656. {
  1657. if (sm == NULL)
  1658. return 0;
  1659. return sm->method_pending == METHOD_PENDING_WAIT;
  1660. }
  1661. /**
  1662. * eap_get_identity - Get the user identity (from EAP-Response/Identity)
  1663. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1664. * @len: Buffer for returning identity length
  1665. * Returns: Pointer to the user identity or %NULL if not available
  1666. */
  1667. const u8 * eap_get_identity(struct eap_sm *sm, size_t *len)
  1668. {
  1669. *len = sm->identity_len;
  1670. return sm->identity;
  1671. }
  1672. /**
  1673. * eap_get_interface - Get pointer to EAP-EAPOL interface data
  1674. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1675. * Returns: Pointer to the EAP-EAPOL interface data
  1676. */
  1677. struct eap_eapol_interface * eap_get_interface(struct eap_sm *sm)
  1678. {
  1679. return &sm->eap_if;
  1680. }
  1681. /**
  1682. * eap_server_clear_identity - Clear EAP identity information
  1683. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1684. *
  1685. * This function can be used to clear the EAP identity information in the EAP
  1686. * server context. This allows the EAP/Identity method to be used again after
  1687. * EAPOL-Start or EAPOL-Logoff.
  1688. */
  1689. void eap_server_clear_identity(struct eap_sm *sm)
  1690. {
  1691. os_free(sm->identity);
  1692. sm->identity = NULL;
  1693. }
  1694. #ifdef CONFIG_TESTING_OPTIONS
  1695. void eap_server_mschap_rx_callback(struct eap_sm *sm, const char *source,
  1696. const u8 *username, size_t username_len,
  1697. const u8 *challenge, const u8 *response)
  1698. {
  1699. char hex_challenge[30], hex_response[90], user[100];
  1700. /* Print out Challenge and Response in format supported by asleap. */
  1701. if (username)
  1702. printf_encode(user, sizeof(user), username, username_len);
  1703. else
  1704. user[0] = '\0';
  1705. wpa_snprintf_hex_sep(hex_challenge, sizeof(hex_challenge),
  1706. challenge, sizeof(challenge), ':');
  1707. wpa_snprintf_hex_sep(hex_response, sizeof(hex_response), response, 24,
  1708. ':');
  1709. wpa_printf(MSG_DEBUG, "[%s/user=%s] asleap -C %s -R %s",
  1710. source, user, hex_challenge, hex_response);
  1711. }
  1712. #endif /* CONFIG_TESTING_OPTIONS */