eap_server.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /*
  2. * hostapd / EAP Full Authenticator state machine (RFC 4137)
  3. * Copyright (c) 2004-2007, 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 "eap_i.h"
  17. #include "state_machine.h"
  18. #include "common/wpa_ctrl.h"
  19. #define STATE_MACHINE_DATA struct eap_sm
  20. #define STATE_MACHINE_DEBUG_PREFIX "EAP"
  21. #define EAP_MAX_AUTH_ROUNDS 50
  22. static void eap_user_free(struct eap_user *user);
  23. /* EAP state machines are described in RFC 4137 */
  24. static int eap_sm_calculateTimeout(struct eap_sm *sm, int retransCount,
  25. int eapSRTT, int eapRTTVAR,
  26. int methodTimeout);
  27. static void eap_sm_parseEapResp(struct eap_sm *sm, const struct wpabuf *resp);
  28. static int eap_sm_getId(const struct wpabuf *data);
  29. static struct wpabuf * eap_sm_buildSuccess(struct eap_sm *sm, u8 id);
  30. static struct wpabuf * eap_sm_buildFailure(struct eap_sm *sm, u8 id);
  31. static int eap_sm_nextId(struct eap_sm *sm, int id);
  32. static void eap_sm_Policy_update(struct eap_sm *sm, const u8 *nak_list,
  33. size_t len);
  34. static EapType eap_sm_Policy_getNextMethod(struct eap_sm *sm, int *vendor);
  35. static int eap_sm_Policy_getDecision(struct eap_sm *sm);
  36. static Boolean eap_sm_Policy_doPickUp(struct eap_sm *sm, EapType method);
  37. static int eap_copy_buf(struct wpabuf **dst, const struct wpabuf *src)
  38. {
  39. if (src == NULL)
  40. return -1;
  41. wpabuf_free(*dst);
  42. *dst = wpabuf_dup(src);
  43. return *dst ? 0 : -1;
  44. }
  45. static int eap_copy_data(u8 **dst, size_t *dst_len,
  46. const u8 *src, size_t src_len)
  47. {
  48. if (src == NULL)
  49. return -1;
  50. os_free(*dst);
  51. *dst = os_malloc(src_len);
  52. if (*dst) {
  53. os_memcpy(*dst, src, src_len);
  54. *dst_len = src_len;
  55. return 0;
  56. } else {
  57. *dst_len = 0;
  58. return -1;
  59. }
  60. }
  61. #define EAP_COPY(dst, src) \
  62. eap_copy_data((dst), (dst ## Len), (src), (src ## Len))
  63. /**
  64. * eap_user_get - Fetch user information from the database
  65. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  66. * @identity: Identity (User-Name) of the user
  67. * @identity_len: Length of identity in bytes
  68. * @phase2: 0 = EAP phase1 user, 1 = EAP phase2 (tunneled) user
  69. * Returns: 0 on success, or -1 on failure
  70. *
  71. * This function is used to fetch user information for EAP. The user will be
  72. * selected based on the specified identity. sm->user and
  73. * sm->user_eap_method_index are updated for the new user when a matching user
  74. * is found. sm->user can be used to get user information (e.g., password).
  75. */
  76. int eap_user_get(struct eap_sm *sm, const u8 *identity, size_t identity_len,
  77. int phase2)
  78. {
  79. struct eap_user *user;
  80. if (sm == NULL || sm->eapol_cb == NULL ||
  81. sm->eapol_cb->get_eap_user == NULL)
  82. return -1;
  83. eap_user_free(sm->user);
  84. sm->user = NULL;
  85. user = os_zalloc(sizeof(*user));
  86. if (user == NULL)
  87. return -1;
  88. if (sm->eapol_cb->get_eap_user(sm->eapol_ctx, identity,
  89. identity_len, phase2, user) != 0) {
  90. eap_user_free(user);
  91. return -1;
  92. }
  93. sm->user = user;
  94. sm->user_eap_method_index = 0;
  95. return 0;
  96. }
  97. void eap_log_msg(struct eap_sm *sm, const char *fmt, ...)
  98. {
  99. va_list ap;
  100. char *buf;
  101. int buflen;
  102. if (sm == NULL || sm->eapol_cb == NULL || sm->eapol_cb->log_msg == NULL)
  103. return;
  104. va_start(ap, fmt);
  105. buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
  106. va_end(ap);
  107. buf = os_malloc(buflen);
  108. if (buf == NULL)
  109. return;
  110. va_start(ap, fmt);
  111. vsnprintf(buf, buflen, fmt, ap);
  112. va_end(ap);
  113. sm->eapol_cb->log_msg(sm->eapol_ctx, buf);
  114. os_free(buf);
  115. }
  116. SM_STATE(EAP, DISABLED)
  117. {
  118. SM_ENTRY(EAP, DISABLED);
  119. sm->num_rounds = 0;
  120. }
  121. SM_STATE(EAP, INITIALIZE)
  122. {
  123. SM_ENTRY(EAP, INITIALIZE);
  124. if (sm->eap_if.eapRestart && !sm->eap_server && sm->identity) {
  125. /*
  126. * Need to allow internal Identity method to be used instead
  127. * of passthrough at the beginning of reauthentication.
  128. */
  129. eap_server_clear_identity(sm);
  130. }
  131. sm->currentId = -1;
  132. sm->eap_if.eapSuccess = FALSE;
  133. sm->eap_if.eapFail = FALSE;
  134. sm->eap_if.eapTimeout = FALSE;
  135. bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
  136. sm->eap_if.eapKeyData = NULL;
  137. sm->eap_if.eapKeyDataLen = 0;
  138. sm->eap_if.eapKeyAvailable = FALSE;
  139. sm->eap_if.eapRestart = FALSE;
  140. /*
  141. * This is not defined in RFC 4137, but method state needs to be
  142. * reseted here so that it does not remain in success state when
  143. * re-authentication starts.
  144. */
  145. if (sm->m && sm->eap_method_priv) {
  146. sm->m->reset(sm, sm->eap_method_priv);
  147. sm->eap_method_priv = NULL;
  148. }
  149. sm->m = NULL;
  150. sm->user_eap_method_index = 0;
  151. if (sm->backend_auth) {
  152. sm->currentMethod = EAP_TYPE_NONE;
  153. /* parse rxResp, respId, respMethod */
  154. eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
  155. if (sm->rxResp) {
  156. sm->currentId = sm->respId;
  157. }
  158. }
  159. sm->num_rounds = 0;
  160. sm->method_pending = METHOD_PENDING_NONE;
  161. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
  162. MACSTR, MAC2STR(sm->peer_addr));
  163. }
  164. SM_STATE(EAP, PICK_UP_METHOD)
  165. {
  166. SM_ENTRY(EAP, PICK_UP_METHOD);
  167. if (eap_sm_Policy_doPickUp(sm, sm->respMethod)) {
  168. sm->currentMethod = sm->respMethod;
  169. if (sm->m && sm->eap_method_priv) {
  170. sm->m->reset(sm, sm->eap_method_priv);
  171. sm->eap_method_priv = NULL;
  172. }
  173. sm->m = eap_server_get_eap_method(EAP_VENDOR_IETF,
  174. sm->currentMethod);
  175. if (sm->m && sm->m->initPickUp) {
  176. sm->eap_method_priv = sm->m->initPickUp(sm);
  177. if (sm->eap_method_priv == NULL) {
  178. wpa_printf(MSG_DEBUG, "EAP: Failed to "
  179. "initialize EAP method %d",
  180. sm->currentMethod);
  181. sm->m = NULL;
  182. sm->currentMethod = EAP_TYPE_NONE;
  183. }
  184. } else {
  185. sm->m = NULL;
  186. sm->currentMethod = EAP_TYPE_NONE;
  187. }
  188. }
  189. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
  190. "method=%u", sm->currentMethod);
  191. }
  192. SM_STATE(EAP, IDLE)
  193. {
  194. SM_ENTRY(EAP, IDLE);
  195. sm->eap_if.retransWhile = eap_sm_calculateTimeout(
  196. sm, sm->retransCount, sm->eap_if.eapSRTT, sm->eap_if.eapRTTVAR,
  197. sm->methodTimeout);
  198. }
  199. SM_STATE(EAP, RETRANSMIT)
  200. {
  201. SM_ENTRY(EAP, RETRANSMIT);
  202. sm->retransCount++;
  203. if (sm->retransCount <= sm->MaxRetrans && sm->lastReqData) {
  204. if (eap_copy_buf(&sm->eap_if.eapReqData, sm->lastReqData) == 0)
  205. sm->eap_if.eapReq = TRUE;
  206. }
  207. }
  208. SM_STATE(EAP, RECEIVED)
  209. {
  210. SM_ENTRY(EAP, RECEIVED);
  211. /* parse rxResp, respId, respMethod */
  212. eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
  213. sm->num_rounds++;
  214. }
  215. SM_STATE(EAP, DISCARD)
  216. {
  217. SM_ENTRY(EAP, DISCARD);
  218. sm->eap_if.eapResp = FALSE;
  219. sm->eap_if.eapNoReq = TRUE;
  220. }
  221. SM_STATE(EAP, SEND_REQUEST)
  222. {
  223. SM_ENTRY(EAP, SEND_REQUEST);
  224. sm->retransCount = 0;
  225. if (sm->eap_if.eapReqData) {
  226. if (eap_copy_buf(&sm->lastReqData, sm->eap_if.eapReqData) == 0)
  227. {
  228. sm->eap_if.eapResp = FALSE;
  229. sm->eap_if.eapReq = TRUE;
  230. } else {
  231. sm->eap_if.eapResp = FALSE;
  232. sm->eap_if.eapReq = FALSE;
  233. }
  234. } else {
  235. wpa_printf(MSG_INFO, "EAP: SEND_REQUEST - no eapReqData");
  236. sm->eap_if.eapResp = FALSE;
  237. sm->eap_if.eapReq = FALSE;
  238. sm->eap_if.eapNoReq = TRUE;
  239. }
  240. }
  241. SM_STATE(EAP, INTEGRITY_CHECK)
  242. {
  243. SM_ENTRY(EAP, INTEGRITY_CHECK);
  244. if (!eap_hdr_len_valid(sm->eap_if.eapRespData, 1)) {
  245. sm->ignore = TRUE;
  246. return;
  247. }
  248. if (sm->m->check) {
  249. sm->ignore = sm->m->check(sm, sm->eap_method_priv,
  250. sm->eap_if.eapRespData);
  251. }
  252. }
  253. SM_STATE(EAP, METHOD_REQUEST)
  254. {
  255. SM_ENTRY(EAP, METHOD_REQUEST);
  256. if (sm->m == NULL) {
  257. wpa_printf(MSG_DEBUG, "EAP: method not initialized");
  258. return;
  259. }
  260. sm->currentId = eap_sm_nextId(sm, sm->currentId);
  261. wpa_printf(MSG_DEBUG, "EAP: building EAP-Request: Identifier %d",
  262. sm->currentId);
  263. sm->lastId = sm->currentId;
  264. wpabuf_free(sm->eap_if.eapReqData);
  265. sm->eap_if.eapReqData = sm->m->buildReq(sm, sm->eap_method_priv,
  266. sm->currentId);
  267. if (sm->m->getTimeout)
  268. sm->methodTimeout = sm->m->getTimeout(sm, sm->eap_method_priv);
  269. else
  270. sm->methodTimeout = 0;
  271. }
  272. SM_STATE(EAP, METHOD_RESPONSE)
  273. {
  274. SM_ENTRY(EAP, METHOD_RESPONSE);
  275. if (!eap_hdr_len_valid(sm->eap_if.eapRespData, 1))
  276. return;
  277. sm->m->process(sm, sm->eap_method_priv, sm->eap_if.eapRespData);
  278. if (sm->m->isDone(sm, sm->eap_method_priv)) {
  279. eap_sm_Policy_update(sm, NULL, 0);
  280. bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
  281. if (sm->m->getKey) {
  282. sm->eap_if.eapKeyData = sm->m->getKey(
  283. sm, sm->eap_method_priv,
  284. &sm->eap_if.eapKeyDataLen);
  285. } else {
  286. sm->eap_if.eapKeyData = NULL;
  287. sm->eap_if.eapKeyDataLen = 0;
  288. }
  289. sm->methodState = METHOD_END;
  290. } else {
  291. sm->methodState = METHOD_CONTINUE;
  292. }
  293. }
  294. SM_STATE(EAP, PROPOSE_METHOD)
  295. {
  296. int vendor;
  297. EapType type;
  298. SM_ENTRY(EAP, PROPOSE_METHOD);
  299. try_another_method:
  300. type = eap_sm_Policy_getNextMethod(sm, &vendor);
  301. if (vendor == EAP_VENDOR_IETF)
  302. sm->currentMethod = type;
  303. else
  304. sm->currentMethod = EAP_TYPE_EXPANDED;
  305. if (sm->m && sm->eap_method_priv) {
  306. sm->m->reset(sm, sm->eap_method_priv);
  307. sm->eap_method_priv = NULL;
  308. }
  309. sm->m = eap_server_get_eap_method(vendor, type);
  310. if (sm->m) {
  311. sm->eap_method_priv = sm->m->init(sm);
  312. if (sm->eap_method_priv == NULL) {
  313. wpa_printf(MSG_DEBUG, "EAP: Failed to initialize EAP "
  314. "method %d", sm->currentMethod);
  315. sm->m = NULL;
  316. sm->currentMethod = EAP_TYPE_NONE;
  317. goto try_another_method;
  318. }
  319. }
  320. if (sm->m == NULL) {
  321. wpa_printf(MSG_DEBUG, "EAP: Could not find suitable EAP method");
  322. eap_log_msg(sm, "Could not find suitable EAP method");
  323. sm->decision = DECISION_FAILURE;
  324. return;
  325. }
  326. if (sm->currentMethod == EAP_TYPE_IDENTITY ||
  327. sm->currentMethod == EAP_TYPE_NOTIFICATION)
  328. sm->methodState = METHOD_CONTINUE;
  329. else
  330. sm->methodState = METHOD_PROPOSED;
  331. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
  332. "vendor=%u method=%u", vendor, sm->currentMethod);
  333. eap_log_msg(sm, "Propose EAP method vendor=%u method=%u",
  334. vendor, sm->currentMethod);
  335. }
  336. SM_STATE(EAP, NAK)
  337. {
  338. const struct eap_hdr *nak;
  339. size_t len = 0;
  340. const u8 *pos;
  341. const u8 *nak_list = NULL;
  342. SM_ENTRY(EAP, NAK);
  343. if (sm->eap_method_priv) {
  344. sm->m->reset(sm, sm->eap_method_priv);
  345. sm->eap_method_priv = NULL;
  346. }
  347. sm->m = NULL;
  348. if (!eap_hdr_len_valid(sm->eap_if.eapRespData, 1))
  349. return;
  350. nak = wpabuf_head(sm->eap_if.eapRespData);
  351. if (nak && wpabuf_len(sm->eap_if.eapRespData) > sizeof(*nak)) {
  352. len = be_to_host16(nak->length);
  353. if (len > wpabuf_len(sm->eap_if.eapRespData))
  354. len = wpabuf_len(sm->eap_if.eapRespData);
  355. pos = (const u8 *) (nak + 1);
  356. len -= sizeof(*nak);
  357. if (*pos == EAP_TYPE_NAK) {
  358. pos++;
  359. len--;
  360. nak_list = pos;
  361. }
  362. }
  363. eap_sm_Policy_update(sm, nak_list, len);
  364. }
  365. SM_STATE(EAP, SELECT_ACTION)
  366. {
  367. SM_ENTRY(EAP, SELECT_ACTION);
  368. sm->decision = eap_sm_Policy_getDecision(sm);
  369. }
  370. SM_STATE(EAP, TIMEOUT_FAILURE)
  371. {
  372. SM_ENTRY(EAP, TIMEOUT_FAILURE);
  373. sm->eap_if.eapTimeout = TRUE;
  374. }
  375. SM_STATE(EAP, FAILURE)
  376. {
  377. SM_ENTRY(EAP, FAILURE);
  378. wpabuf_free(sm->eap_if.eapReqData);
  379. sm->eap_if.eapReqData = eap_sm_buildFailure(sm, sm->currentId);
  380. wpabuf_free(sm->lastReqData);
  381. sm->lastReqData = NULL;
  382. sm->eap_if.eapFail = TRUE;
  383. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
  384. MACSTR, MAC2STR(sm->peer_addr));
  385. }
  386. SM_STATE(EAP, SUCCESS)
  387. {
  388. SM_ENTRY(EAP, SUCCESS);
  389. wpabuf_free(sm->eap_if.eapReqData);
  390. sm->eap_if.eapReqData = eap_sm_buildSuccess(sm, sm->currentId);
  391. wpabuf_free(sm->lastReqData);
  392. sm->lastReqData = NULL;
  393. if (sm->eap_if.eapKeyData)
  394. sm->eap_if.eapKeyAvailable = TRUE;
  395. sm->eap_if.eapSuccess = TRUE;
  396. wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
  397. MACSTR, MAC2STR(sm->peer_addr));
  398. }
  399. SM_STATE(EAP, INITIALIZE_PASSTHROUGH)
  400. {
  401. SM_ENTRY(EAP, INITIALIZE_PASSTHROUGH);
  402. wpabuf_free(sm->eap_if.aaaEapRespData);
  403. sm->eap_if.aaaEapRespData = NULL;
  404. }
  405. SM_STATE(EAP, IDLE2)
  406. {
  407. SM_ENTRY(EAP, IDLE2);
  408. sm->eap_if.retransWhile = eap_sm_calculateTimeout(
  409. sm, sm->retransCount, sm->eap_if.eapSRTT, sm->eap_if.eapRTTVAR,
  410. sm->methodTimeout);
  411. }
  412. SM_STATE(EAP, RETRANSMIT2)
  413. {
  414. SM_ENTRY(EAP, RETRANSMIT2);
  415. sm->retransCount++;
  416. if (sm->retransCount <= sm->MaxRetrans && sm->lastReqData) {
  417. if (eap_copy_buf(&sm->eap_if.eapReqData, sm->lastReqData) == 0)
  418. sm->eap_if.eapReq = TRUE;
  419. }
  420. }
  421. SM_STATE(EAP, RECEIVED2)
  422. {
  423. SM_ENTRY(EAP, RECEIVED2);
  424. /* parse rxResp, respId, respMethod */
  425. eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
  426. }
  427. SM_STATE(EAP, DISCARD2)
  428. {
  429. SM_ENTRY(EAP, DISCARD2);
  430. sm->eap_if.eapResp = FALSE;
  431. sm->eap_if.eapNoReq = TRUE;
  432. }
  433. SM_STATE(EAP, SEND_REQUEST2)
  434. {
  435. SM_ENTRY(EAP, SEND_REQUEST2);
  436. sm->retransCount = 0;
  437. if (sm->eap_if.eapReqData) {
  438. if (eap_copy_buf(&sm->lastReqData, sm->eap_if.eapReqData) == 0)
  439. {
  440. sm->eap_if.eapResp = FALSE;
  441. sm->eap_if.eapReq = TRUE;
  442. } else {
  443. sm->eap_if.eapResp = FALSE;
  444. sm->eap_if.eapReq = FALSE;
  445. }
  446. } else {
  447. wpa_printf(MSG_INFO, "EAP: SEND_REQUEST2 - no eapReqData");
  448. sm->eap_if.eapResp = FALSE;
  449. sm->eap_if.eapReq = FALSE;
  450. sm->eap_if.eapNoReq = TRUE;
  451. }
  452. }
  453. SM_STATE(EAP, AAA_REQUEST)
  454. {
  455. SM_ENTRY(EAP, AAA_REQUEST);
  456. if (sm->eap_if.eapRespData == NULL) {
  457. wpa_printf(MSG_INFO, "EAP: AAA_REQUEST - no eapRespData");
  458. return;
  459. }
  460. /*
  461. * if (respMethod == IDENTITY)
  462. * aaaIdentity = eapRespData
  463. * This is already taken care of by the EAP-Identity method which
  464. * stores the identity into sm->identity.
  465. */
  466. eap_copy_buf(&sm->eap_if.aaaEapRespData, sm->eap_if.eapRespData);
  467. }
  468. SM_STATE(EAP, AAA_RESPONSE)
  469. {
  470. SM_ENTRY(EAP, AAA_RESPONSE);
  471. eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
  472. sm->currentId = eap_sm_getId(sm->eap_if.eapReqData);
  473. sm->methodTimeout = sm->eap_if.aaaMethodTimeout;
  474. }
  475. SM_STATE(EAP, AAA_IDLE)
  476. {
  477. SM_ENTRY(EAP, AAA_IDLE);
  478. sm->eap_if.aaaFail = FALSE;
  479. sm->eap_if.aaaSuccess = FALSE;
  480. sm->eap_if.aaaEapReq = FALSE;
  481. sm->eap_if.aaaEapNoReq = FALSE;
  482. sm->eap_if.aaaEapResp = TRUE;
  483. }
  484. SM_STATE(EAP, TIMEOUT_FAILURE2)
  485. {
  486. SM_ENTRY(EAP, TIMEOUT_FAILURE2);
  487. sm->eap_if.eapTimeout = TRUE;
  488. }
  489. SM_STATE(EAP, FAILURE2)
  490. {
  491. SM_ENTRY(EAP, FAILURE2);
  492. eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
  493. sm->eap_if.eapFail = TRUE;
  494. }
  495. SM_STATE(EAP, SUCCESS2)
  496. {
  497. SM_ENTRY(EAP, SUCCESS2);
  498. eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
  499. sm->eap_if.eapKeyAvailable = sm->eap_if.aaaEapKeyAvailable;
  500. if (sm->eap_if.aaaEapKeyAvailable) {
  501. EAP_COPY(&sm->eap_if.eapKeyData, sm->eap_if.aaaEapKeyData);
  502. } else {
  503. bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
  504. sm->eap_if.eapKeyData = NULL;
  505. sm->eap_if.eapKeyDataLen = 0;
  506. }
  507. sm->eap_if.eapSuccess = TRUE;
  508. /*
  509. * Start reauthentication with identity request even though we know the
  510. * previously used identity. This is needed to get reauthentication
  511. * started properly.
  512. */
  513. sm->start_reauth = TRUE;
  514. }
  515. SM_STEP(EAP)
  516. {
  517. if (sm->eap_if.eapRestart && sm->eap_if.portEnabled)
  518. SM_ENTER_GLOBAL(EAP, INITIALIZE);
  519. else if (!sm->eap_if.portEnabled)
  520. SM_ENTER_GLOBAL(EAP, DISABLED);
  521. else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
  522. if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
  523. wpa_printf(MSG_DEBUG, "EAP: more than %d "
  524. "authentication rounds - abort",
  525. EAP_MAX_AUTH_ROUNDS);
  526. sm->num_rounds++;
  527. SM_ENTER_GLOBAL(EAP, FAILURE);
  528. }
  529. } else switch (sm->EAP_state) {
  530. case EAP_INITIALIZE:
  531. if (sm->backend_auth) {
  532. if (!sm->rxResp)
  533. SM_ENTER(EAP, SELECT_ACTION);
  534. else if (sm->rxResp &&
  535. (sm->respMethod == EAP_TYPE_NAK ||
  536. (sm->respMethod == EAP_TYPE_EXPANDED &&
  537. sm->respVendor == EAP_VENDOR_IETF &&
  538. sm->respVendorMethod == EAP_TYPE_NAK)))
  539. SM_ENTER(EAP, NAK);
  540. else
  541. SM_ENTER(EAP, PICK_UP_METHOD);
  542. } else {
  543. SM_ENTER(EAP, SELECT_ACTION);
  544. }
  545. break;
  546. case EAP_PICK_UP_METHOD:
  547. if (sm->currentMethod == EAP_TYPE_NONE) {
  548. SM_ENTER(EAP, SELECT_ACTION);
  549. } else {
  550. SM_ENTER(EAP, METHOD_RESPONSE);
  551. }
  552. break;
  553. case EAP_DISABLED:
  554. if (sm->eap_if.portEnabled)
  555. SM_ENTER(EAP, INITIALIZE);
  556. break;
  557. case EAP_IDLE:
  558. if (sm->eap_if.retransWhile == 0)
  559. SM_ENTER(EAP, RETRANSMIT);
  560. else if (sm->eap_if.eapResp)
  561. SM_ENTER(EAP, RECEIVED);
  562. break;
  563. case EAP_RETRANSMIT:
  564. if (sm->retransCount > sm->MaxRetrans)
  565. SM_ENTER(EAP, TIMEOUT_FAILURE);
  566. else
  567. SM_ENTER(EAP, IDLE);
  568. break;
  569. case EAP_RECEIVED:
  570. if (sm->rxResp && (sm->respId == sm->currentId) &&
  571. (sm->respMethod == EAP_TYPE_NAK ||
  572. (sm->respMethod == EAP_TYPE_EXPANDED &&
  573. sm->respVendor == EAP_VENDOR_IETF &&
  574. sm->respVendorMethod == EAP_TYPE_NAK))
  575. && (sm->methodState == METHOD_PROPOSED))
  576. SM_ENTER(EAP, NAK);
  577. else if (sm->rxResp && (sm->respId == sm->currentId) &&
  578. ((sm->respMethod == sm->currentMethod) ||
  579. (sm->respMethod == EAP_TYPE_EXPANDED &&
  580. sm->respVendor == EAP_VENDOR_IETF &&
  581. sm->respVendorMethod == sm->currentMethod)))
  582. SM_ENTER(EAP, INTEGRITY_CHECK);
  583. else {
  584. wpa_printf(MSG_DEBUG, "EAP: RECEIVED->DISCARD: "
  585. "rxResp=%d respId=%d currentId=%d "
  586. "respMethod=%d currentMethod=%d",
  587. sm->rxResp, sm->respId, sm->currentId,
  588. sm->respMethod, sm->currentMethod);
  589. eap_log_msg(sm, "Discard received EAP message");
  590. SM_ENTER(EAP, DISCARD);
  591. }
  592. break;
  593. case EAP_DISCARD:
  594. SM_ENTER(EAP, IDLE);
  595. break;
  596. case EAP_SEND_REQUEST:
  597. SM_ENTER(EAP, IDLE);
  598. break;
  599. case EAP_INTEGRITY_CHECK:
  600. if (sm->ignore)
  601. SM_ENTER(EAP, DISCARD);
  602. else
  603. SM_ENTER(EAP, METHOD_RESPONSE);
  604. break;
  605. case EAP_METHOD_REQUEST:
  606. if (sm->m == NULL) {
  607. /*
  608. * This transition is not mentioned in RFC 4137, but it
  609. * is needed to handle cleanly a case where EAP method
  610. * initialization fails.
  611. */
  612. SM_ENTER(EAP, FAILURE);
  613. break;
  614. }
  615. SM_ENTER(EAP, SEND_REQUEST);
  616. break;
  617. case EAP_METHOD_RESPONSE:
  618. /*
  619. * Note: Mechanism to allow EAP methods to wait while going
  620. * through pending processing is an extension to RFC 4137
  621. * which only defines the transits to SELECT_ACTION and
  622. * METHOD_REQUEST from this METHOD_RESPONSE state.
  623. */
  624. if (sm->methodState == METHOD_END)
  625. SM_ENTER(EAP, SELECT_ACTION);
  626. else if (sm->method_pending == METHOD_PENDING_WAIT) {
  627. wpa_printf(MSG_DEBUG, "EAP: Method has pending "
  628. "processing - wait before proceeding to "
  629. "METHOD_REQUEST state");
  630. } else if (sm->method_pending == METHOD_PENDING_CONT) {
  631. wpa_printf(MSG_DEBUG, "EAP: Method has completed "
  632. "pending processing - reprocess pending "
  633. "EAP message");
  634. sm->method_pending = METHOD_PENDING_NONE;
  635. SM_ENTER(EAP, METHOD_RESPONSE);
  636. } else
  637. SM_ENTER(EAP, METHOD_REQUEST);
  638. break;
  639. case EAP_PROPOSE_METHOD:
  640. /*
  641. * Note: Mechanism to allow EAP methods to wait while going
  642. * through pending processing is an extension to RFC 4137
  643. * which only defines the transit to METHOD_REQUEST from this
  644. * PROPOSE_METHOD state.
  645. */
  646. if (sm->method_pending == METHOD_PENDING_WAIT) {
  647. wpa_printf(MSG_DEBUG, "EAP: Method has pending "
  648. "processing - wait before proceeding to "
  649. "METHOD_REQUEST state");
  650. if (sm->user_eap_method_index > 0)
  651. sm->user_eap_method_index--;
  652. } else if (sm->method_pending == METHOD_PENDING_CONT) {
  653. wpa_printf(MSG_DEBUG, "EAP: Method has completed "
  654. "pending processing - reprocess pending "
  655. "EAP message");
  656. sm->method_pending = METHOD_PENDING_NONE;
  657. SM_ENTER(EAP, PROPOSE_METHOD);
  658. } else
  659. SM_ENTER(EAP, METHOD_REQUEST);
  660. break;
  661. case EAP_NAK:
  662. SM_ENTER(EAP, SELECT_ACTION);
  663. break;
  664. case EAP_SELECT_ACTION:
  665. if (sm->decision == DECISION_FAILURE)
  666. SM_ENTER(EAP, FAILURE);
  667. else if (sm->decision == DECISION_SUCCESS)
  668. SM_ENTER(EAP, SUCCESS);
  669. else if (sm->decision == DECISION_PASSTHROUGH)
  670. SM_ENTER(EAP, INITIALIZE_PASSTHROUGH);
  671. else
  672. SM_ENTER(EAP, PROPOSE_METHOD);
  673. break;
  674. case EAP_TIMEOUT_FAILURE:
  675. break;
  676. case EAP_FAILURE:
  677. break;
  678. case EAP_SUCCESS:
  679. break;
  680. case EAP_INITIALIZE_PASSTHROUGH:
  681. if (sm->currentId == -1)
  682. SM_ENTER(EAP, AAA_IDLE);
  683. else
  684. SM_ENTER(EAP, AAA_REQUEST);
  685. break;
  686. case EAP_IDLE2:
  687. if (sm->eap_if.eapResp)
  688. SM_ENTER(EAP, RECEIVED2);
  689. else if (sm->eap_if.retransWhile == 0)
  690. SM_ENTER(EAP, RETRANSMIT2);
  691. break;
  692. case EAP_RETRANSMIT2:
  693. if (sm->retransCount > sm->MaxRetrans)
  694. SM_ENTER(EAP, TIMEOUT_FAILURE2);
  695. else
  696. SM_ENTER(EAP, IDLE2);
  697. break;
  698. case EAP_RECEIVED2:
  699. if (sm->rxResp && (sm->respId == sm->currentId))
  700. SM_ENTER(EAP, AAA_REQUEST);
  701. else
  702. SM_ENTER(EAP, DISCARD2);
  703. break;
  704. case EAP_DISCARD2:
  705. SM_ENTER(EAP, IDLE2);
  706. break;
  707. case EAP_SEND_REQUEST2:
  708. SM_ENTER(EAP, IDLE2);
  709. break;
  710. case EAP_AAA_REQUEST:
  711. SM_ENTER(EAP, AAA_IDLE);
  712. break;
  713. case EAP_AAA_RESPONSE:
  714. SM_ENTER(EAP, SEND_REQUEST2);
  715. break;
  716. case EAP_AAA_IDLE:
  717. if (sm->eap_if.aaaFail)
  718. SM_ENTER(EAP, FAILURE2);
  719. else if (sm->eap_if.aaaSuccess)
  720. SM_ENTER(EAP, SUCCESS2);
  721. else if (sm->eap_if.aaaEapReq)
  722. SM_ENTER(EAP, AAA_RESPONSE);
  723. else if (sm->eap_if.aaaTimeout)
  724. SM_ENTER(EAP, TIMEOUT_FAILURE2);
  725. break;
  726. case EAP_TIMEOUT_FAILURE2:
  727. break;
  728. case EAP_FAILURE2:
  729. break;
  730. case EAP_SUCCESS2:
  731. break;
  732. }
  733. }
  734. static int eap_sm_calculateTimeout(struct eap_sm *sm, int retransCount,
  735. int eapSRTT, int eapRTTVAR,
  736. int methodTimeout)
  737. {
  738. int rto, i;
  739. if (methodTimeout) {
  740. /*
  741. * EAP method (either internal or through AAA server, provided
  742. * timeout hint. Use that as-is as a timeout for retransmitting
  743. * the EAP request if no response is received.
  744. */
  745. wpa_printf(MSG_DEBUG, "EAP: retransmit timeout %d seconds "
  746. "(from EAP method hint)", methodTimeout);
  747. return methodTimeout;
  748. }
  749. /*
  750. * RFC 3748 recommends algorithms described in RFC 2988 for estimation
  751. * of the retransmission timeout. This should be implemented once
  752. * round-trip time measurements are available. For nowm a simple
  753. * backoff mechanism is used instead if there are no EAP method
  754. * specific hints.
  755. *
  756. * SRTT = smoothed round-trip time
  757. * RTTVAR = round-trip time variation
  758. * RTO = retransmission timeout
  759. */
  760. /*
  761. * RFC 2988, 2.1: before RTT measurement, set RTO to 3 seconds for
  762. * initial retransmission and then double the RTO to provide back off
  763. * per 5.5. Limit the maximum RTO to 20 seconds per RFC 3748, 4.3
  764. * modified RTOmax.
  765. */
  766. rto = 3;
  767. for (i = 0; i < retransCount; i++) {
  768. rto *= 2;
  769. if (rto >= 20) {
  770. rto = 20;
  771. break;
  772. }
  773. }
  774. wpa_printf(MSG_DEBUG, "EAP: retransmit timeout %d seconds "
  775. "(from dynamic back off; retransCount=%d)",
  776. rto, retransCount);
  777. return rto;
  778. }
  779. static void eap_sm_parseEapResp(struct eap_sm *sm, const struct wpabuf *resp)
  780. {
  781. const struct eap_hdr *hdr;
  782. size_t plen;
  783. /* parse rxResp, respId, respMethod */
  784. sm->rxResp = FALSE;
  785. sm->respId = -1;
  786. sm->respMethod = EAP_TYPE_NONE;
  787. sm->respVendor = EAP_VENDOR_IETF;
  788. sm->respVendorMethod = EAP_TYPE_NONE;
  789. if (resp == NULL || wpabuf_len(resp) < sizeof(*hdr)) {
  790. wpa_printf(MSG_DEBUG, "EAP: parseEapResp: invalid resp=%p "
  791. "len=%lu", resp,
  792. resp ? (unsigned long) wpabuf_len(resp) : 0);
  793. return;
  794. }
  795. hdr = wpabuf_head(resp);
  796. plen = be_to_host16(hdr->length);
  797. if (plen > wpabuf_len(resp)) {
  798. wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
  799. "(len=%lu plen=%lu)",
  800. (unsigned long) wpabuf_len(resp),
  801. (unsigned long) plen);
  802. return;
  803. }
  804. sm->respId = hdr->identifier;
  805. if (hdr->code == EAP_CODE_RESPONSE)
  806. sm->rxResp = TRUE;
  807. if (plen > sizeof(*hdr)) {
  808. u8 *pos = (u8 *) (hdr + 1);
  809. sm->respMethod = *pos++;
  810. if (sm->respMethod == EAP_TYPE_EXPANDED) {
  811. if (plen < sizeof(*hdr) + 8) {
  812. wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
  813. "expanded EAP-Packet (plen=%lu)",
  814. (unsigned long) plen);
  815. return;
  816. }
  817. sm->respVendor = WPA_GET_BE24(pos);
  818. pos += 3;
  819. sm->respVendorMethod = WPA_GET_BE32(pos);
  820. }
  821. }
  822. wpa_printf(MSG_DEBUG, "EAP: parseEapResp: rxResp=%d respId=%d "
  823. "respMethod=%u respVendor=%u respVendorMethod=%u",
  824. sm->rxResp, sm->respId, sm->respMethod, sm->respVendor,
  825. sm->respVendorMethod);
  826. }
  827. static int eap_sm_getId(const struct wpabuf *data)
  828. {
  829. const struct eap_hdr *hdr;
  830. if (data == NULL || wpabuf_len(data) < sizeof(*hdr))
  831. return -1;
  832. hdr = wpabuf_head(data);
  833. wpa_printf(MSG_DEBUG, "EAP: getId: id=%d", hdr->identifier);
  834. return hdr->identifier;
  835. }
  836. static struct wpabuf * eap_sm_buildSuccess(struct eap_sm *sm, u8 id)
  837. {
  838. struct wpabuf *msg;
  839. struct eap_hdr *resp;
  840. wpa_printf(MSG_DEBUG, "EAP: Building EAP-Success (id=%d)", id);
  841. msg = wpabuf_alloc(sizeof(*resp));
  842. if (msg == NULL)
  843. return NULL;
  844. resp = wpabuf_put(msg, sizeof(*resp));
  845. resp->code = EAP_CODE_SUCCESS;
  846. resp->identifier = id;
  847. resp->length = host_to_be16(sizeof(*resp));
  848. return msg;
  849. }
  850. static struct wpabuf * eap_sm_buildFailure(struct eap_sm *sm, u8 id)
  851. {
  852. struct wpabuf *msg;
  853. struct eap_hdr *resp;
  854. wpa_printf(MSG_DEBUG, "EAP: Building EAP-Failure (id=%d)", id);
  855. msg = wpabuf_alloc(sizeof(*resp));
  856. if (msg == NULL)
  857. return NULL;
  858. resp = wpabuf_put(msg, sizeof(*resp));
  859. resp->code = EAP_CODE_FAILURE;
  860. resp->identifier = id;
  861. resp->length = host_to_be16(sizeof(*resp));
  862. return msg;
  863. }
  864. static int eap_sm_nextId(struct eap_sm *sm, int id)
  865. {
  866. if (id < 0) {
  867. /* RFC 3748 Ch 4.1: recommended to initialize Identifier with a
  868. * random number */
  869. id = rand() & 0xff;
  870. if (id != sm->lastId)
  871. return id;
  872. }
  873. return (id + 1) & 0xff;
  874. }
  875. /**
  876. * eap_sm_process_nak - Process EAP-Response/Nak
  877. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  878. * @nak_list: Nak list (allowed methods) from the supplicant
  879. * @len: Length of nak_list in bytes
  880. *
  881. * This function is called when EAP-Response/Nak is received from the
  882. * supplicant. This can happen for both phase 1 and phase 2 authentications.
  883. */
  884. void eap_sm_process_nak(struct eap_sm *sm, const u8 *nak_list, size_t len)
  885. {
  886. int i;
  887. size_t j;
  888. if (sm->user == NULL)
  889. return;
  890. wpa_printf(MSG_MSGDUMP, "EAP: processing NAK (current EAP method "
  891. "index %d)", sm->user_eap_method_index);
  892. wpa_hexdump(MSG_MSGDUMP, "EAP: configured methods",
  893. (u8 *) sm->user->methods,
  894. EAP_MAX_METHODS * sizeof(sm->user->methods[0]));
  895. wpa_hexdump(MSG_MSGDUMP, "EAP: list of methods supported by the peer",
  896. nak_list, len);
  897. i = sm->user_eap_method_index;
  898. while (i < EAP_MAX_METHODS &&
  899. (sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
  900. sm->user->methods[i].method != EAP_TYPE_NONE)) {
  901. if (sm->user->methods[i].vendor != EAP_VENDOR_IETF)
  902. goto not_found;
  903. for (j = 0; j < len; j++) {
  904. if (nak_list[j] == sm->user->methods[i].method) {
  905. break;
  906. }
  907. }
  908. if (j < len) {
  909. /* found */
  910. i++;
  911. continue;
  912. }
  913. not_found:
  914. /* not found - remove from the list */
  915. if (i + 1 < EAP_MAX_METHODS) {
  916. os_memmove(&sm->user->methods[i],
  917. &sm->user->methods[i + 1],
  918. (EAP_MAX_METHODS - i - 1) *
  919. sizeof(sm->user->methods[0]));
  920. }
  921. sm->user->methods[EAP_MAX_METHODS - 1].vendor =
  922. EAP_VENDOR_IETF;
  923. sm->user->methods[EAP_MAX_METHODS - 1].method = EAP_TYPE_NONE;
  924. }
  925. wpa_hexdump(MSG_MSGDUMP, "EAP: new list of configured methods",
  926. (u8 *) sm->user->methods, EAP_MAX_METHODS *
  927. sizeof(sm->user->methods[0]));
  928. }
  929. static void eap_sm_Policy_update(struct eap_sm *sm, const u8 *nak_list,
  930. size_t len)
  931. {
  932. if (nak_list == NULL || sm == NULL || sm->user == NULL)
  933. return;
  934. if (sm->user->phase2) {
  935. wpa_printf(MSG_DEBUG, "EAP: EAP-Nak received after Phase2 user"
  936. " info was selected - reject");
  937. sm->decision = DECISION_FAILURE;
  938. return;
  939. }
  940. eap_sm_process_nak(sm, nak_list, len);
  941. }
  942. static EapType eap_sm_Policy_getNextMethod(struct eap_sm *sm, int *vendor)
  943. {
  944. EapType next;
  945. int idx = sm->user_eap_method_index;
  946. /* In theory, there should be no problems with starting
  947. * re-authentication with something else than EAP-Request/Identity and
  948. * this does indeed work with wpa_supplicant. However, at least Funk
  949. * Supplicant seemed to ignore re-auth if it skipped
  950. * EAP-Request/Identity.
  951. * Re-auth sets currentId == -1, so that can be used here to select
  952. * whether Identity needs to be requested again. */
  953. if (sm->identity == NULL || sm->currentId == -1) {
  954. *vendor = EAP_VENDOR_IETF;
  955. next = EAP_TYPE_IDENTITY;
  956. sm->update_user = TRUE;
  957. } else if (sm->user && idx < EAP_MAX_METHODS &&
  958. (sm->user->methods[idx].vendor != EAP_VENDOR_IETF ||
  959. sm->user->methods[idx].method != EAP_TYPE_NONE)) {
  960. *vendor = sm->user->methods[idx].vendor;
  961. next = sm->user->methods[idx].method;
  962. sm->user_eap_method_index++;
  963. } else {
  964. *vendor = EAP_VENDOR_IETF;
  965. next = EAP_TYPE_NONE;
  966. }
  967. wpa_printf(MSG_DEBUG, "EAP: getNextMethod: vendor %d type %d",
  968. *vendor, next);
  969. return next;
  970. }
  971. static int eap_sm_Policy_getDecision(struct eap_sm *sm)
  972. {
  973. if (!sm->eap_server && sm->identity && !sm->start_reauth) {
  974. wpa_printf(MSG_DEBUG, "EAP: getDecision: -> PASSTHROUGH");
  975. return DECISION_PASSTHROUGH;
  976. }
  977. if (sm->m && sm->currentMethod != EAP_TYPE_IDENTITY &&
  978. sm->m->isSuccess(sm, sm->eap_method_priv)) {
  979. wpa_printf(MSG_DEBUG, "EAP: getDecision: method succeeded -> "
  980. "SUCCESS");
  981. sm->update_user = TRUE;
  982. return DECISION_SUCCESS;
  983. }
  984. if (sm->m && sm->m->isDone(sm, sm->eap_method_priv) &&
  985. !sm->m->isSuccess(sm, sm->eap_method_priv)) {
  986. wpa_printf(MSG_DEBUG, "EAP: getDecision: method failed -> "
  987. "FAILURE");
  988. sm->update_user = TRUE;
  989. return DECISION_FAILURE;
  990. }
  991. if ((sm->user == NULL || sm->update_user) && sm->identity &&
  992. !sm->start_reauth) {
  993. /*
  994. * Allow Identity method to be started once to allow identity
  995. * selection hint to be sent from the authentication server,
  996. * but prevent a loop of Identity requests by only allowing
  997. * this to happen once.
  998. */
  999. int id_req = 0;
  1000. if (sm->user && sm->currentMethod == EAP_TYPE_IDENTITY &&
  1001. sm->user->methods[0].vendor == EAP_VENDOR_IETF &&
  1002. sm->user->methods[0].method == EAP_TYPE_IDENTITY)
  1003. id_req = 1;
  1004. if (eap_user_get(sm, sm->identity, sm->identity_len, 0) != 0) {
  1005. wpa_printf(MSG_DEBUG, "EAP: getDecision: user not "
  1006. "found from database -> FAILURE");
  1007. return DECISION_FAILURE;
  1008. }
  1009. if (id_req && sm->user &&
  1010. sm->user->methods[0].vendor == EAP_VENDOR_IETF &&
  1011. sm->user->methods[0].method == EAP_TYPE_IDENTITY) {
  1012. wpa_printf(MSG_DEBUG, "EAP: getDecision: stop "
  1013. "identity request loop -> FAILURE");
  1014. sm->update_user = TRUE;
  1015. return DECISION_FAILURE;
  1016. }
  1017. sm->update_user = FALSE;
  1018. }
  1019. sm->start_reauth = FALSE;
  1020. if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
  1021. (sm->user->methods[sm->user_eap_method_index].vendor !=
  1022. EAP_VENDOR_IETF ||
  1023. sm->user->methods[sm->user_eap_method_index].method !=
  1024. EAP_TYPE_NONE)) {
  1025. wpa_printf(MSG_DEBUG, "EAP: getDecision: another method "
  1026. "available -> CONTINUE");
  1027. return DECISION_CONTINUE;
  1028. }
  1029. if (sm->identity == NULL || sm->currentId == -1) {
  1030. wpa_printf(MSG_DEBUG, "EAP: getDecision: no identity known "
  1031. "yet -> CONTINUE");
  1032. return DECISION_CONTINUE;
  1033. }
  1034. wpa_printf(MSG_DEBUG, "EAP: getDecision: no more methods available -> "
  1035. "FAILURE");
  1036. return DECISION_FAILURE;
  1037. }
  1038. static Boolean eap_sm_Policy_doPickUp(struct eap_sm *sm, EapType method)
  1039. {
  1040. return method == EAP_TYPE_IDENTITY ? TRUE : FALSE;
  1041. }
  1042. /**
  1043. * eap_server_sm_step - Step EAP server state machine
  1044. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1045. * Returns: 1 if EAP state was changed or 0 if not
  1046. *
  1047. * This function advances EAP state machine to a new state to match with the
  1048. * current variables. This should be called whenever variables used by the EAP
  1049. * state machine have changed.
  1050. */
  1051. int eap_server_sm_step(struct eap_sm *sm)
  1052. {
  1053. int res = 0;
  1054. do {
  1055. sm->changed = FALSE;
  1056. SM_STEP_RUN(EAP);
  1057. if (sm->changed)
  1058. res = 1;
  1059. } while (sm->changed);
  1060. return res;
  1061. }
  1062. static void eap_user_free(struct eap_user *user)
  1063. {
  1064. if (user == NULL)
  1065. return;
  1066. bin_clear_free(user->password, user->password_len);
  1067. user->password = NULL;
  1068. os_free(user);
  1069. }
  1070. /**
  1071. * eap_server_sm_init - Allocate and initialize EAP server state machine
  1072. * @eapol_ctx: Context data to be used with eapol_cb calls
  1073. * @eapol_cb: Pointer to EAPOL callback functions
  1074. * @conf: EAP configuration
  1075. * Returns: Pointer to the allocated EAP state machine or %NULL on failure
  1076. *
  1077. * This function allocates and initializes an EAP state machine.
  1078. */
  1079. struct eap_sm * eap_server_sm_init(void *eapol_ctx,
  1080. struct eapol_callbacks *eapol_cb,
  1081. struct eap_config *conf)
  1082. {
  1083. struct eap_sm *sm;
  1084. sm = os_zalloc(sizeof(*sm));
  1085. if (sm == NULL)
  1086. return NULL;
  1087. sm->eapol_ctx = eapol_ctx;
  1088. sm->eapol_cb = eapol_cb;
  1089. sm->MaxRetrans = 5; /* RFC 3748: max 3-5 retransmissions suggested */
  1090. sm->ssl_ctx = conf->ssl_ctx;
  1091. sm->msg_ctx = conf->msg_ctx;
  1092. sm->eap_sim_db_priv = conf->eap_sim_db_priv;
  1093. sm->backend_auth = conf->backend_auth;
  1094. sm->eap_server = conf->eap_server;
  1095. if (conf->pac_opaque_encr_key) {
  1096. sm->pac_opaque_encr_key = os_malloc(16);
  1097. if (sm->pac_opaque_encr_key) {
  1098. os_memcpy(sm->pac_opaque_encr_key,
  1099. conf->pac_opaque_encr_key, 16);
  1100. }
  1101. }
  1102. if (conf->eap_fast_a_id) {
  1103. sm->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
  1104. if (sm->eap_fast_a_id) {
  1105. os_memcpy(sm->eap_fast_a_id, conf->eap_fast_a_id,
  1106. conf->eap_fast_a_id_len);
  1107. sm->eap_fast_a_id_len = conf->eap_fast_a_id_len;
  1108. }
  1109. }
  1110. if (conf->eap_fast_a_id_info)
  1111. sm->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
  1112. sm->eap_fast_prov = conf->eap_fast_prov;
  1113. sm->pac_key_lifetime = conf->pac_key_lifetime;
  1114. sm->pac_key_refresh_time = conf->pac_key_refresh_time;
  1115. sm->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
  1116. sm->tnc = conf->tnc;
  1117. sm->wps = conf->wps;
  1118. if (conf->assoc_wps_ie)
  1119. sm->assoc_wps_ie = wpabuf_dup(conf->assoc_wps_ie);
  1120. if (conf->assoc_p2p_ie)
  1121. sm->assoc_p2p_ie = wpabuf_dup(conf->assoc_p2p_ie);
  1122. if (conf->peer_addr)
  1123. os_memcpy(sm->peer_addr, conf->peer_addr, ETH_ALEN);
  1124. sm->fragment_size = conf->fragment_size;
  1125. sm->pwd_group = conf->pwd_group;
  1126. sm->pbc_in_m1 = conf->pbc_in_m1;
  1127. sm->server_id = conf->server_id;
  1128. sm->server_id_len = conf->server_id_len;
  1129. #ifdef CONFIG_TESTING_OPTIONS
  1130. sm->tls_test_flags = conf->tls_test_flags;
  1131. #endif /* CONFIG_TESTING_OPTIONS */
  1132. wpa_printf(MSG_DEBUG, "EAP: Server state machine created");
  1133. return sm;
  1134. }
  1135. /**
  1136. * eap_server_sm_deinit - Deinitialize and free an EAP server state machine
  1137. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1138. *
  1139. * This function deinitializes EAP state machine and frees all allocated
  1140. * resources.
  1141. */
  1142. void eap_server_sm_deinit(struct eap_sm *sm)
  1143. {
  1144. if (sm == NULL)
  1145. return;
  1146. wpa_printf(MSG_DEBUG, "EAP: Server state machine removed");
  1147. if (sm->m && sm->eap_method_priv)
  1148. sm->m->reset(sm, sm->eap_method_priv);
  1149. wpabuf_free(sm->eap_if.eapReqData);
  1150. bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
  1151. wpabuf_free(sm->lastReqData);
  1152. wpabuf_free(sm->eap_if.eapRespData);
  1153. os_free(sm->identity);
  1154. os_free(sm->pac_opaque_encr_key);
  1155. os_free(sm->eap_fast_a_id);
  1156. os_free(sm->eap_fast_a_id_info);
  1157. wpabuf_free(sm->eap_if.aaaEapReqData);
  1158. wpabuf_free(sm->eap_if.aaaEapRespData);
  1159. bin_clear_free(sm->eap_if.aaaEapKeyData, sm->eap_if.aaaEapKeyDataLen);
  1160. eap_user_free(sm->user);
  1161. wpabuf_free(sm->assoc_wps_ie);
  1162. wpabuf_free(sm->assoc_p2p_ie);
  1163. os_free(sm);
  1164. }
  1165. /**
  1166. * eap_sm_notify_cached - Notify EAP state machine of cached PMK
  1167. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1168. *
  1169. * This function is called when PMKSA caching is used to skip EAP
  1170. * authentication.
  1171. */
  1172. void eap_sm_notify_cached(struct eap_sm *sm)
  1173. {
  1174. if (sm == NULL)
  1175. return;
  1176. sm->EAP_state = EAP_SUCCESS;
  1177. }
  1178. /**
  1179. * eap_sm_pending_cb - EAP state machine callback for a pending EAP request
  1180. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1181. *
  1182. * This function is called when data for a pending EAP-Request is received.
  1183. */
  1184. void eap_sm_pending_cb(struct eap_sm *sm)
  1185. {
  1186. if (sm == NULL)
  1187. return;
  1188. wpa_printf(MSG_DEBUG, "EAP: Callback for pending request received");
  1189. if (sm->method_pending == METHOD_PENDING_WAIT)
  1190. sm->method_pending = METHOD_PENDING_CONT;
  1191. }
  1192. /**
  1193. * eap_sm_method_pending - Query whether EAP method is waiting for pending data
  1194. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1195. * Returns: 1 if method is waiting for pending data or 0 if not
  1196. */
  1197. int eap_sm_method_pending(struct eap_sm *sm)
  1198. {
  1199. if (sm == NULL)
  1200. return 0;
  1201. return sm->method_pending == METHOD_PENDING_WAIT;
  1202. }
  1203. /**
  1204. * eap_get_identity - Get the user identity (from EAP-Response/Identity)
  1205. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1206. * @len: Buffer for returning identity length
  1207. * Returns: Pointer to the user identity or %NULL if not available
  1208. */
  1209. const u8 * eap_get_identity(struct eap_sm *sm, size_t *len)
  1210. {
  1211. *len = sm->identity_len;
  1212. return sm->identity;
  1213. }
  1214. /**
  1215. * eap_get_interface - Get pointer to EAP-EAPOL interface data
  1216. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1217. * Returns: Pointer to the EAP-EAPOL interface data
  1218. */
  1219. struct eap_eapol_interface * eap_get_interface(struct eap_sm *sm)
  1220. {
  1221. return &sm->eap_if;
  1222. }
  1223. /**
  1224. * eap_server_clear_identity - Clear EAP identity information
  1225. * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
  1226. *
  1227. * This function can be used to clear the EAP identity information in the EAP
  1228. * server context. This allows the EAP/Identity method to be used again after
  1229. * EAPOL-Start or EAPOL-Logoff.
  1230. */
  1231. void eap_server_clear_identity(struct eap_sm *sm)
  1232. {
  1233. os_free(sm->identity);
  1234. sm->identity = NULL;
  1235. }