eap_server.c 35 KB

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