eap_server.c 36 KB

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