eap_server.c 52 KB

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