dd9a8be3ddffa41ade9ae5dfe9de4e30eaa46295
[libeap.git] / src / eap_peer / eap.c
1 /*
2  * EAP peer state machines (RFC 4137)
3  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  *
14  * This file implements the Peer State Machine as defined in RFC 4137. The used
15  * states and state transitions match mostly with the RFC. However, there are
16  * couple of additional transitions for working around small issues noticed
17  * during testing. These exceptions are explained in comments within the
18  * functions in this file. The method functions, m.func(), are similar to the
19  * ones used in RFC 4137, but some small changes have used here to optimize
20  * operations and to add functionality needed for fast re-authentication
21  * (session resumption).
22  */
23
24 #include "includes.h"
25
26 #include "common.h"
27 #include "eap_i.h"
28 #include "eap_config.h"
29 #include "tls.h"
30 #include "crypto.h"
31 #include "pcsc_funcs.h"
32 #include "common/wpa_ctrl.h"
33 #include "state_machine.h"
34 #include "eap_common/eap_wsc_common.h"
35
36 #define STATE_MACHINE_DATA struct eap_sm
37 #define STATE_MACHINE_DEBUG_PREFIX "EAP"
38
39 #define EAP_MAX_AUTH_ROUNDS 50
40
41
42 static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
43                                   EapType method);
44 static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id);
45 static void eap_sm_processIdentity(struct eap_sm *sm,
46                                    const struct wpabuf *req);
47 static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req);
48 static struct wpabuf * eap_sm_buildNotify(int id);
49 static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req);
50 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
51 static const char * eap_sm_method_state_txt(EapMethodState state);
52 static const char * eap_sm_decision_txt(EapDecision decision);
53 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
54
55
56
57 static Boolean eapol_get_bool(struct eap_sm *sm, enum eapol_bool_var var)
58 {
59         return sm->eapol_cb->get_bool(sm->eapol_ctx, var);
60 }
61
62
63 static void eapol_set_bool(struct eap_sm *sm, enum eapol_bool_var var,
64                            Boolean value)
65 {
66         sm->eapol_cb->set_bool(sm->eapol_ctx, var, value);
67 }
68
69
70 static unsigned int eapol_get_int(struct eap_sm *sm, enum eapol_int_var var)
71 {
72         return sm->eapol_cb->get_int(sm->eapol_ctx, var);
73 }
74
75
76 static void eapol_set_int(struct eap_sm *sm, enum eapol_int_var var,
77                           unsigned int value)
78 {
79         sm->eapol_cb->set_int(sm->eapol_ctx, var, value);
80 }
81
82
83 static struct wpabuf * eapol_get_eapReqData(struct eap_sm *sm)
84 {
85         return sm->eapol_cb->get_eapReqData(sm->eapol_ctx);
86 }
87
88
89 static void eap_deinit_prev_method(struct eap_sm *sm, const char *txt)
90 {
91         if (sm->m == NULL || sm->eap_method_priv == NULL)
92                 return;
93
94         wpa_printf(MSG_DEBUG, "EAP: deinitialize previously used EAP method "
95                    "(%d, %s) at %s", sm->selectedMethod, sm->m->name, txt);
96         sm->m->deinit(sm, sm->eap_method_priv);
97         sm->eap_method_priv = NULL;
98         sm->m = NULL;
99 }
100
101
102 /**
103  * eap_allowed_method - Check whether EAP method is allowed
104  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
105  * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
106  * @method: EAP type
107  * Returns: 1 = allowed EAP method, 0 = not allowed
108  */
109 int eap_allowed_method(struct eap_sm *sm, int vendor, u32 method)
110 {
111         struct eap_peer_config *config = eap_get_config(sm);
112         int i;
113         struct eap_method_type *m;
114
115         if (config == NULL || config->eap_methods == NULL)
116                 return 1;
117
118         m = config->eap_methods;
119         for (i = 0; m[i].vendor != EAP_VENDOR_IETF ||
120                      m[i].method != EAP_TYPE_NONE; i++) {
121                 if (m[i].vendor == vendor && m[i].method == method)
122                         return 1;
123         }
124         return 0;
125 }
126
127
128 /*
129  * This state initializes state machine variables when the machine is
130  * activated (portEnabled = TRUE). This is also used when re-starting
131  * authentication (eapRestart == TRUE).
132  */
133 SM_STATE(EAP, INITIALIZE)
134 {
135         SM_ENTRY(EAP, INITIALIZE);
136         if (sm->fast_reauth && sm->m && sm->m->has_reauth_data &&
137             sm->m->has_reauth_data(sm, sm->eap_method_priv) &&
138             !sm->prev_failure) {
139                 wpa_printf(MSG_DEBUG, "EAP: maintaining EAP method data for "
140                            "fast reauthentication");
141                 sm->m->deinit_for_reauth(sm, sm->eap_method_priv);
142         } else {
143                 eap_deinit_prev_method(sm, "INITIALIZE");
144         }
145         sm->selectedMethod = EAP_TYPE_NONE;
146         sm->methodState = METHOD_NONE;
147         sm->allowNotifications = TRUE;
148         sm->decision = DECISION_FAIL;
149         eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
150         eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
151         eapol_set_bool(sm, EAPOL_eapFail, FALSE);
152         os_free(sm->eapKeyData);
153         sm->eapKeyData = NULL;
154         sm->eapKeyAvailable = FALSE;
155         eapol_set_bool(sm, EAPOL_eapRestart, FALSE);
156         sm->lastId = -1; /* new session - make sure this does not match with
157                           * the first EAP-Packet */
158         /*
159          * RFC 4137 does not reset eapResp and eapNoResp here. However, this
160          * seemed to be able to trigger cases where both were set and if EAPOL
161          * state machine uses eapNoResp first, it may end up not sending a real
162          * reply correctly. This occurred when the workaround in FAIL state set
163          * eapNoResp = TRUE.. Maybe that workaround needs to be fixed to do
164          * something else(?)
165          */
166         eapol_set_bool(sm, EAPOL_eapResp, FALSE);
167         eapol_set_bool(sm, EAPOL_eapNoResp, FALSE);
168         sm->num_rounds = 0;
169         sm->prev_failure = 0;
170 }
171
172
173 /*
174  * This state is reached whenever service from the lower layer is interrupted
175  * or unavailable (portEnabled == FALSE). Immediate transition to INITIALIZE
176  * occurs when the port becomes enabled.
177  */
178 SM_STATE(EAP, DISABLED)
179 {
180         SM_ENTRY(EAP, DISABLED);
181         sm->num_rounds = 0;
182 }
183
184
185 /*
186  * The state machine spends most of its time here, waiting for something to
187  * happen. This state is entered unconditionally from INITIALIZE, DISCARD, and
188  * SEND_RESPONSE states.
189  */
190 SM_STATE(EAP, IDLE)
191 {
192         SM_ENTRY(EAP, IDLE);
193 }
194
195
196 /*
197  * This state is entered when an EAP packet is received (eapReq == TRUE) to
198  * parse the packet header.
199  */
200 SM_STATE(EAP, RECEIVED)
201 {
202         const struct wpabuf *eapReqData;
203
204         SM_ENTRY(EAP, RECEIVED);
205         eapReqData = eapol_get_eapReqData(sm);
206         /* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */
207         eap_sm_parseEapReq(sm, eapReqData);
208         sm->num_rounds++;
209 }
210
211
212 /*
213  * This state is entered when a request for a new type comes in. Either the
214  * correct method is started, or a Nak response is built.
215  */
216 SM_STATE(EAP, GET_METHOD)
217 {
218         int reinit;
219         EapType method;
220
221         SM_ENTRY(EAP, GET_METHOD);
222
223         if (sm->reqMethod == EAP_TYPE_EXPANDED)
224                 method = sm->reqVendorMethod;
225         else
226                 method = sm->reqMethod;
227
228         if (!eap_sm_allowMethod(sm, sm->reqVendor, method)) {
229                 wpa_printf(MSG_DEBUG, "EAP: vendor %u method %u not allowed",
230                            sm->reqVendor, method);
231                 goto nak;
232         }
233
234         /*
235          * RFC 4137 does not define specific operation for fast
236          * re-authentication (session resumption). The design here is to allow
237          * the previously used method data to be maintained for
238          * re-authentication if the method support session resumption.
239          * Otherwise, the previously used method data is freed and a new method
240          * is allocated here.
241          */
242         if (sm->fast_reauth &&
243             sm->m && sm->m->vendor == sm->reqVendor &&
244             sm->m->method == method &&
245             sm->m->has_reauth_data &&
246             sm->m->has_reauth_data(sm, sm->eap_method_priv)) {
247                 wpa_printf(MSG_DEBUG, "EAP: Using previous method data"
248                            " for fast re-authentication");
249                 reinit = 1;
250         } else {
251                 eap_deinit_prev_method(sm, "GET_METHOD");
252                 reinit = 0;
253         }
254
255         sm->selectedMethod = sm->reqMethod;
256         if (sm->m == NULL)
257                 sm->m = eap_peer_get_eap_method(sm->reqVendor, method);
258         if (!sm->m) {
259                 wpa_printf(MSG_DEBUG, "EAP: Could not find selected method: "
260                            "vendor %d method %d",
261                            sm->reqVendor, method);
262                 goto nak;
263         }
264
265         wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP method: "
266                    "vendor %u method %u (%s)",
267                    sm->reqVendor, method, sm->m->name);
268         if (reinit)
269                 sm->eap_method_priv = sm->m->init_for_reauth(
270                         sm, sm->eap_method_priv);
271         else
272                 sm->eap_method_priv = sm->m->init(sm);
273
274         if (sm->eap_method_priv == NULL) {
275                 struct eap_peer_config *config = eap_get_config(sm);
276                 wpa_msg(sm->msg_ctx, MSG_INFO,
277                         "EAP: Failed to initialize EAP method: vendor %u "
278                         "method %u (%s)",
279                         sm->reqVendor, method, sm->m->name);
280                 sm->m = NULL;
281                 sm->methodState = METHOD_NONE;
282                 sm->selectedMethod = EAP_TYPE_NONE;
283                 if (sm->reqMethod == EAP_TYPE_TLS && config &&
284                     (config->pending_req_pin ||
285                      config->pending_req_passphrase)) {
286                         /*
287                          * Return without generating Nak in order to allow
288                          * entering of PIN code or passphrase to retry the
289                          * current EAP packet.
290                          */
291                         wpa_printf(MSG_DEBUG, "EAP: Pending PIN/passphrase "
292                                    "request - skip Nak");
293                         return;
294                 }
295
296                 goto nak;
297         }
298
299         sm->methodState = METHOD_INIT;
300         wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_METHOD
301                 "EAP vendor %u method %u (%s) selected",
302                 sm->reqVendor, method, sm->m->name);
303         return;
304
305 nak:
306         wpabuf_free(sm->eapRespData);
307         sm->eapRespData = NULL;
308         sm->eapRespData = eap_sm_buildNak(sm, sm->reqId);
309 }
310
311
312 /*
313  * The method processing happens here. The request from the authenticator is
314  * processed, and an appropriate response packet is built.
315  */
316 SM_STATE(EAP, METHOD)
317 {
318         struct wpabuf *eapReqData;
319         struct eap_method_ret ret;
320
321         SM_ENTRY(EAP, METHOD);
322         if (sm->m == NULL) {
323                 wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected");
324                 return;
325         }
326
327         eapReqData = eapol_get_eapReqData(sm);
328
329         /*
330          * Get ignore, methodState, decision, allowNotifications, and
331          * eapRespData. RFC 4137 uses three separate method procedure (check,
332          * process, and buildResp) in this state. These have been combined into
333          * a single function call to m->process() in order to optimize EAP
334          * method implementation interface a bit. These procedures are only
335          * used from within this METHOD state, so there is no need to keep
336          * these as separate C functions.
337          *
338          * The RFC 4137 procedures return values as follows:
339          * ignore = m.check(eapReqData)
340          * (methodState, decision, allowNotifications) = m.process(eapReqData)
341          * eapRespData = m.buildResp(reqId)
342          */
343         os_memset(&ret, 0, sizeof(ret));
344         ret.ignore = sm->ignore;
345         ret.methodState = sm->methodState;
346         ret.decision = sm->decision;
347         ret.allowNotifications = sm->allowNotifications;
348         wpabuf_free(sm->eapRespData);
349         sm->eapRespData = NULL;
350         sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret,
351                                          eapReqData);
352         wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s "
353                    "methodState=%s decision=%s",
354                    ret.ignore ? "TRUE" : "FALSE",
355                    eap_sm_method_state_txt(ret.methodState),
356                    eap_sm_decision_txt(ret.decision));
357
358         sm->ignore = ret.ignore;
359         if (sm->ignore)
360                 return;
361         sm->methodState = ret.methodState;
362         sm->decision = ret.decision;
363         sm->allowNotifications = ret.allowNotifications;
364
365         if (sm->m->isKeyAvailable && sm->m->getKey &&
366             sm->m->isKeyAvailable(sm, sm->eap_method_priv)) {
367                 os_free(sm->eapKeyData);
368                 sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv,
369                                                &sm->eapKeyDataLen);
370         }
371 }
372
373
374 /*
375  * This state signals the lower layer that a response packet is ready to be
376  * sent.
377  */
378 SM_STATE(EAP, SEND_RESPONSE)
379 {
380         SM_ENTRY(EAP, SEND_RESPONSE);
381         wpabuf_free(sm->lastRespData);
382         if (sm->eapRespData) {
383                 if (sm->workaround)
384                         os_memcpy(sm->last_md5, sm->req_md5, 16);
385                 sm->lastId = sm->reqId;
386                 sm->lastRespData = wpabuf_dup(sm->eapRespData);
387                 eapol_set_bool(sm, EAPOL_eapResp, TRUE);
388         } else
389                 sm->lastRespData = NULL;
390         eapol_set_bool(sm, EAPOL_eapReq, FALSE);
391         eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
392 }
393
394
395 /*
396  * This state signals the lower layer that the request was discarded, and no
397  * response packet will be sent at this time.
398  */
399 SM_STATE(EAP, DISCARD)
400 {
401         SM_ENTRY(EAP, DISCARD);
402         eapol_set_bool(sm, EAPOL_eapReq, FALSE);
403         eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
404 }
405
406
407 /*
408  * Handles requests for Identity method and builds a response.
409  */
410 SM_STATE(EAP, IDENTITY)
411 {
412         const struct wpabuf *eapReqData;
413
414         SM_ENTRY(EAP, IDENTITY);
415         eapReqData = eapol_get_eapReqData(sm);
416         eap_sm_processIdentity(sm, eapReqData);
417         wpabuf_free(sm->eapRespData);
418         sm->eapRespData = NULL;
419         sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId, 0);
420 }
421
422
423 /*
424  * Handles requests for Notification method and builds a response.
425  */
426 SM_STATE(EAP, NOTIFICATION)
427 {
428         const struct wpabuf *eapReqData;
429
430         SM_ENTRY(EAP, NOTIFICATION);
431         eapReqData = eapol_get_eapReqData(sm);
432         eap_sm_processNotify(sm, eapReqData);
433         wpabuf_free(sm->eapRespData);
434         sm->eapRespData = NULL;
435         sm->eapRespData = eap_sm_buildNotify(sm->reqId);
436 }
437
438
439 /*
440  * This state retransmits the previous response packet.
441  */
442 SM_STATE(EAP, RETRANSMIT)
443 {
444         SM_ENTRY(EAP, RETRANSMIT);
445         wpabuf_free(sm->eapRespData);
446         if (sm->lastRespData)
447                 sm->eapRespData = wpabuf_dup(sm->lastRespData);
448         else
449                 sm->eapRespData = NULL;
450 }
451
452
453 /*
454  * This state is entered in case of a successful completion of authentication
455  * and state machine waits here until port is disabled or EAP authentication is
456  * restarted.
457  */
458 SM_STATE(EAP, SUCCESS)
459 {
460         SM_ENTRY(EAP, SUCCESS);
461         if (sm->eapKeyData != NULL)
462                 sm->eapKeyAvailable = TRUE;
463         eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
464
465         /*
466          * RFC 4137 does not clear eapReq here, but this seems to be required
467          * to avoid processing the same request twice when state machine is
468          * initialized.
469          */
470         eapol_set_bool(sm, EAPOL_eapReq, FALSE);
471
472         /*
473          * RFC 4137 does not set eapNoResp here, but this seems to be required
474          * to get EAPOL Supplicant backend state machine into SUCCESS state. In
475          * addition, either eapResp or eapNoResp is required to be set after
476          * processing the received EAP frame.
477          */
478         eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
479
480         wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
481                 "EAP authentication completed successfully");
482 }
483
484
485 /*
486  * This state is entered in case of a failure and state machine waits here
487  * until port is disabled or EAP authentication is restarted.
488  */
489 SM_STATE(EAP, FAILURE)
490 {
491         SM_ENTRY(EAP, FAILURE);
492         eapol_set_bool(sm, EAPOL_eapFail, TRUE);
493
494         /*
495          * RFC 4137 does not clear eapReq here, but this seems to be required
496          * to avoid processing the same request twice when state machine is
497          * initialized.
498          */
499         eapol_set_bool(sm, EAPOL_eapReq, FALSE);
500
501         /*
502          * RFC 4137 does not set eapNoResp here. However, either eapResp or
503          * eapNoResp is required to be set after processing the received EAP
504          * frame.
505          */
506         eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
507
508         wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
509                 "EAP authentication failed");
510
511         sm->prev_failure = 1;
512 }
513
514
515 static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId)
516 {
517         /*
518          * At least Microsoft IAS and Meetinghouse Aegis seem to be sending
519          * EAP-Success/Failure with lastId + 1 even though RFC 3748 and
520          * RFC 4137 require that reqId == lastId. In addition, it looks like
521          * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
522          *
523          * Accept this kind of Id if EAP workarounds are enabled. These are
524          * unauthenticated plaintext messages, so this should have minimal
525          * security implications (bit easier to fake EAP-Success/Failure).
526          */
527         if (sm->workaround && (reqId == ((lastId + 1) & 0xff) ||
528                                reqId == ((lastId + 2) & 0xff))) {
529                 wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected "
530                            "identifier field in EAP Success: "
531                            "reqId=%d lastId=%d (these are supposed to be "
532                            "same)", reqId, lastId);
533                 return 1;
534         }
535         wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d "
536                    "lastId=%d", reqId, lastId);
537         return 0;
538 }
539
540
541 /*
542  * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
543  */
544
545 static void eap_peer_sm_step_idle(struct eap_sm *sm)
546 {
547         /*
548          * The first three transitions are from RFC 4137. The last two are
549          * local additions to handle special cases with LEAP and PEAP server
550          * not sending EAP-Success in some cases.
551          */
552         if (eapol_get_bool(sm, EAPOL_eapReq))
553                 SM_ENTER(EAP, RECEIVED);
554         else if ((eapol_get_bool(sm, EAPOL_altAccept) &&
555                   sm->decision != DECISION_FAIL) ||
556                  (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
557                   sm->decision == DECISION_UNCOND_SUCC))
558                 SM_ENTER(EAP, SUCCESS);
559         else if (eapol_get_bool(sm, EAPOL_altReject) ||
560                  (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
561                   sm->decision != DECISION_UNCOND_SUCC) ||
562                  (eapol_get_bool(sm, EAPOL_altAccept) &&
563                   sm->methodState != METHOD_CONT &&
564                   sm->decision == DECISION_FAIL))
565                 SM_ENTER(EAP, FAILURE);
566         else if (sm->selectedMethod == EAP_TYPE_LEAP &&
567                  sm->leap_done && sm->decision != DECISION_FAIL &&
568                  sm->methodState == METHOD_DONE)
569                 SM_ENTER(EAP, SUCCESS);
570         else if (sm->selectedMethod == EAP_TYPE_PEAP &&
571                  sm->peap_done && sm->decision != DECISION_FAIL &&
572                  sm->methodState == METHOD_DONE)
573                 SM_ENTER(EAP, SUCCESS);
574 }
575
576
577 static int eap_peer_req_is_duplicate(struct eap_sm *sm)
578 {
579         int duplicate;
580
581         duplicate = (sm->reqId == sm->lastId) && sm->rxReq;
582         if (sm->workaround && duplicate &&
583             os_memcmp(sm->req_md5, sm->last_md5, 16) != 0) {
584                 /*
585                  * RFC 4137 uses (reqId == lastId) as the only verification for
586                  * duplicate EAP requests. However, this misses cases where the
587                  * AS is incorrectly using the same id again; and
588                  * unfortunately, such implementations exist. Use MD5 hash as
589                  * an extra verification for the packets being duplicate to
590                  * workaround these issues.
591                  */
592                 wpa_printf(MSG_DEBUG, "EAP: AS used the same Id again, but "
593                            "EAP packets were not identical");
594                 wpa_printf(MSG_DEBUG, "EAP: workaround - assume this is not a "
595                            "duplicate packet");
596                 duplicate = 0;
597         }
598
599         return duplicate;
600 }
601
602
603 static void eap_peer_sm_step_received(struct eap_sm *sm)
604 {
605         int duplicate = eap_peer_req_is_duplicate(sm);
606
607         /*
608          * Two special cases below for LEAP are local additions to work around
609          * odd LEAP behavior (EAP-Success in the middle of authentication and
610          * then swapped roles). Other transitions are based on RFC 4137.
611          */
612         if (sm->rxSuccess && sm->decision != DECISION_FAIL &&
613             (sm->reqId == sm->lastId ||
614              eap_success_workaround(sm, sm->reqId, sm->lastId)))
615                 SM_ENTER(EAP, SUCCESS);
616         else if (sm->methodState != METHOD_CONT &&
617                  ((sm->rxFailure &&
618                    sm->decision != DECISION_UNCOND_SUCC) ||
619                   (sm->rxSuccess && sm->decision == DECISION_FAIL &&
620                    (sm->selectedMethod != EAP_TYPE_LEAP ||
621                     sm->methodState != METHOD_MAY_CONT))) &&
622                  (sm->reqId == sm->lastId ||
623                   eap_success_workaround(sm, sm->reqId, sm->lastId)))
624                 SM_ENTER(EAP, FAILURE);
625         else if (sm->rxReq && duplicate)
626                 SM_ENTER(EAP, RETRANSMIT);
627         else if (sm->rxReq && !duplicate &&
628                  sm->reqMethod == EAP_TYPE_NOTIFICATION &&
629                  sm->allowNotifications)
630                 SM_ENTER(EAP, NOTIFICATION);
631         else if (sm->rxReq && !duplicate &&
632                  sm->selectedMethod == EAP_TYPE_NONE &&
633                  sm->reqMethod == EAP_TYPE_IDENTITY)
634                 SM_ENTER(EAP, IDENTITY);
635         else if (sm->rxReq && !duplicate &&
636                  sm->selectedMethod == EAP_TYPE_NONE &&
637                  sm->reqMethod != EAP_TYPE_IDENTITY &&
638                  sm->reqMethod != EAP_TYPE_NOTIFICATION)
639                 SM_ENTER(EAP, GET_METHOD);
640         else if (sm->rxReq && !duplicate &&
641                  sm->reqMethod == sm->selectedMethod &&
642                  sm->methodState != METHOD_DONE)
643                 SM_ENTER(EAP, METHOD);
644         else if (sm->selectedMethod == EAP_TYPE_LEAP &&
645                  (sm->rxSuccess || sm->rxResp))
646                 SM_ENTER(EAP, METHOD);
647         else
648                 SM_ENTER(EAP, DISCARD);
649 }
650
651
652 static void eap_peer_sm_step_local(struct eap_sm *sm)
653 {
654         switch (sm->EAP_state) {
655         case EAP_INITIALIZE:
656                 SM_ENTER(EAP, IDLE);
657                 break;
658         case EAP_DISABLED:
659                 if (eapol_get_bool(sm, EAPOL_portEnabled) &&
660                     !sm->force_disabled)
661                         SM_ENTER(EAP, INITIALIZE);
662                 break;
663         case EAP_IDLE:
664                 eap_peer_sm_step_idle(sm);
665                 break;
666         case EAP_RECEIVED:
667                 eap_peer_sm_step_received(sm);
668                 break;
669         case EAP_GET_METHOD:
670                 if (sm->selectedMethod == sm->reqMethod)
671                         SM_ENTER(EAP, METHOD);
672                 else
673                         SM_ENTER(EAP, SEND_RESPONSE);
674                 break;
675         case EAP_METHOD:
676                 if (sm->ignore)
677                         SM_ENTER(EAP, DISCARD);
678                 else
679                         SM_ENTER(EAP, SEND_RESPONSE);
680                 break;
681         case EAP_SEND_RESPONSE:
682                 SM_ENTER(EAP, IDLE);
683                 break;
684         case EAP_DISCARD:
685                 SM_ENTER(EAP, IDLE);
686                 break;
687         case EAP_IDENTITY:
688                 SM_ENTER(EAP, SEND_RESPONSE);
689                 break;
690         case EAP_NOTIFICATION:
691                 SM_ENTER(EAP, SEND_RESPONSE);
692                 break;
693         case EAP_RETRANSMIT:
694                 SM_ENTER(EAP, SEND_RESPONSE);
695                 break;
696         case EAP_SUCCESS:
697                 break;
698         case EAP_FAILURE:
699                 break;
700         }
701 }
702
703
704 SM_STEP(EAP)
705 {
706         /* Global transitions */
707         if (eapol_get_bool(sm, EAPOL_eapRestart) &&
708             eapol_get_bool(sm, EAPOL_portEnabled))
709                 SM_ENTER_GLOBAL(EAP, INITIALIZE);
710         else if (!eapol_get_bool(sm, EAPOL_portEnabled) || sm->force_disabled)
711                 SM_ENTER_GLOBAL(EAP, DISABLED);
712         else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
713                 /* RFC 4137 does not place any limit on number of EAP messages
714                  * in an authentication session. However, some error cases have
715                  * ended up in a state were EAP messages were sent between the
716                  * peer and server in a loop (e.g., TLS ACK frame in both
717                  * direction). Since this is quite undesired outcome, limit the
718                  * total number of EAP round-trips and abort authentication if
719                  * this limit is exceeded.
720                  */
721                 if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
722                         wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: more than %d "
723                                 "authentication rounds - abort",
724                                 EAP_MAX_AUTH_ROUNDS);
725                         sm->num_rounds++;
726                         SM_ENTER_GLOBAL(EAP, FAILURE);
727                 }
728         } else {
729                 /* Local transitions */
730                 eap_peer_sm_step_local(sm);
731         }
732 }
733
734
735 static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
736                                   EapType method)
737 {
738         if (!eap_allowed_method(sm, vendor, method)) {
739                 wpa_printf(MSG_DEBUG, "EAP: configuration does not allow: "
740                            "vendor %u method %u", vendor, method);
741                 return FALSE;
742         }
743         if (eap_peer_get_eap_method(vendor, method))
744                 return TRUE;
745         wpa_printf(MSG_DEBUG, "EAP: not included in build: "
746                    "vendor %u method %u", vendor, method);
747         return FALSE;
748 }
749
750
751 static struct wpabuf * eap_sm_build_expanded_nak(
752         struct eap_sm *sm, int id, const struct eap_method *methods,
753         size_t count)
754 {
755         struct wpabuf *resp;
756         int found = 0;
757         const struct eap_method *m;
758
759         wpa_printf(MSG_DEBUG, "EAP: Building expanded EAP-Nak");
760
761         /* RFC 3748 - 5.3.2: Expanded Nak */
762         resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_EXPANDED,
763                              8 + 8 * (count + 1), EAP_CODE_RESPONSE, id);
764         if (resp == NULL)
765                 return NULL;
766
767         wpabuf_put_be24(resp, EAP_VENDOR_IETF);
768         wpabuf_put_be32(resp, EAP_TYPE_NAK);
769
770         for (m = methods; m; m = m->next) {
771                 if (sm->reqVendor == m->vendor &&
772                     sm->reqVendorMethod == m->method)
773                         continue; /* do not allow the current method again */
774                 if (eap_allowed_method(sm, m->vendor, m->method)) {
775                         wpa_printf(MSG_DEBUG, "EAP: allowed type: "
776                                    "vendor=%u method=%u",
777                                    m->vendor, m->method);
778                         wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
779                         wpabuf_put_be24(resp, m->vendor);
780                         wpabuf_put_be32(resp, m->method);
781
782                         found++;
783                 }
784         }
785         if (!found) {
786                 wpa_printf(MSG_DEBUG, "EAP: no more allowed methods");
787                 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
788                 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
789                 wpabuf_put_be32(resp, EAP_TYPE_NONE);
790         }
791
792         eap_update_len(resp);
793
794         return resp;
795 }
796
797
798 static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id)
799 {
800         struct wpabuf *resp;
801         u8 *start;
802         int found = 0, expanded_found = 0;
803         size_t count;
804         const struct eap_method *methods, *m;
805
806         wpa_printf(MSG_DEBUG, "EAP: Building EAP-Nak (requested type %u "
807                    "vendor=%u method=%u not allowed)", sm->reqMethod,
808                    sm->reqVendor, sm->reqVendorMethod);
809         methods = eap_peer_get_methods(&count);
810         if (methods == NULL)
811                 return NULL;
812         if (sm->reqMethod == EAP_TYPE_EXPANDED)
813                 return eap_sm_build_expanded_nak(sm, id, methods, count);
814
815         /* RFC 3748 - 5.3.1: Legacy Nak */
816         resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK,
817                              sizeof(struct eap_hdr) + 1 + count + 1,
818                              EAP_CODE_RESPONSE, id);
819         if (resp == NULL)
820                 return NULL;
821
822         start = wpabuf_put(resp, 0);
823         for (m = methods; m; m = m->next) {
824                 if (m->vendor == EAP_VENDOR_IETF && m->method == sm->reqMethod)
825                         continue; /* do not allow the current method again */
826                 if (eap_allowed_method(sm, m->vendor, m->method)) {
827                         if (m->vendor != EAP_VENDOR_IETF) {
828                                 if (expanded_found)
829                                         continue;
830                                 expanded_found = 1;
831                                 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
832                         } else
833                                 wpabuf_put_u8(resp, m->method);
834                         found++;
835                 }
836         }
837         if (!found)
838                 wpabuf_put_u8(resp, EAP_TYPE_NONE);
839         wpa_hexdump(MSG_DEBUG, "EAP: allowed methods", start, found);
840
841         eap_update_len(resp);
842
843         return resp;
844 }
845
846
847 static void eap_sm_processIdentity(struct eap_sm *sm, const struct wpabuf *req)
848 {
849         const struct eap_hdr *hdr = wpabuf_head(req);
850         const u8 *pos = (const u8 *) (hdr + 1);
851         pos++;
852
853         wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
854                 "EAP authentication started");
855
856         /*
857          * RFC 3748 - 5.1: Identity
858          * Data field may contain a displayable message in UTF-8. If this
859          * includes NUL-character, only the data before that should be
860          * displayed. Some EAP implementasitons may piggy-back additional
861          * options after the NUL.
862          */
863         /* TODO: could save displayable message so that it can be shown to the
864          * user in case of interaction is required */
865         wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Identity data",
866                           pos, be_to_host16(hdr->length) - 5);
867 }
868
869
870 #ifdef PCSC_FUNCS
871 static int eap_sm_imsi_identity(struct eap_sm *sm,
872                                 struct eap_peer_config *conf)
873 {
874         int aka = 0;
875         char imsi[100];
876         size_t imsi_len;
877         struct eap_method_type *m = conf->eap_methods;
878         int i;
879
880         imsi_len = sizeof(imsi);
881         if (scard_get_imsi(sm->scard_ctx, imsi, &imsi_len)) {
882                 wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM");
883                 return -1;
884         }
885
886         wpa_hexdump_ascii(MSG_DEBUG, "IMSI", (u8 *) imsi, imsi_len);
887
888         for (i = 0; m && (m[i].vendor != EAP_VENDOR_IETF ||
889                           m[i].method != EAP_TYPE_NONE); i++) {
890                 if (m[i].vendor == EAP_VENDOR_IETF &&
891                     m[i].method == EAP_TYPE_AKA) {
892                         aka = 1;
893                         break;
894                 }
895         }
896
897         os_free(conf->identity);
898         conf->identity = os_malloc(1 + imsi_len);
899         if (conf->identity == NULL) {
900                 wpa_printf(MSG_WARNING, "Failed to allocate buffer for "
901                            "IMSI-based identity");
902                 return -1;
903         }
904
905         conf->identity[0] = aka ? '0' : '1';
906         os_memcpy(conf->identity + 1, imsi, imsi_len);
907         conf->identity_len = 1 + imsi_len;
908
909         return 0;
910 }
911 #endif /* PCSC_FUNCS */
912
913
914 static int eap_sm_set_scard_pin(struct eap_sm *sm,
915                                 struct eap_peer_config *conf)
916 {
917 #ifdef PCSC_FUNCS
918         if (scard_set_pin(sm->scard_ctx, conf->pin)) {
919                 /*
920                  * Make sure the same PIN is not tried again in order to avoid
921                  * blocking SIM.
922                  */
923                 os_free(conf->pin);
924                 conf->pin = NULL;
925
926                 wpa_printf(MSG_WARNING, "PIN validation failed");
927                 eap_sm_request_pin(sm);
928                 return -1;
929         }
930         return 0;
931 #else /* PCSC_FUNCS */
932         return -1;
933 #endif /* PCSC_FUNCS */
934 }
935
936 static int eap_sm_get_scard_identity(struct eap_sm *sm,
937                                      struct eap_peer_config *conf)
938 {
939 #ifdef PCSC_FUNCS
940         if (eap_sm_set_scard_pin(sm, conf))
941                 return -1;
942
943         return eap_sm_imsi_identity(sm, conf);
944 #else /* PCSC_FUNCS */
945         return -1;
946 #endif /* PCSC_FUNCS */
947 }
948
949
950 /**
951  * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
952  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
953  * @id: EAP identifier for the packet
954  * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
955  * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
956  * failure
957  *
958  * This function allocates and builds an EAP-Identity/Response packet for the
959  * current network. The caller is responsible for freeing the returned data.
960  */
961 struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted)
962 {
963         struct eap_peer_config *config = eap_get_config(sm);
964         struct wpabuf *resp;
965         const u8 *identity;
966         size_t identity_len;
967
968         if (config == NULL) {
969                 wpa_printf(MSG_WARNING, "EAP: buildIdentity: configuration "
970                            "was not available");
971                 return NULL;
972         }
973
974         if (sm->m && sm->m->get_identity &&
975             (identity = sm->m->get_identity(sm, sm->eap_method_priv,
976                                             &identity_len)) != NULL) {
977                 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth "
978                                   "identity", identity, identity_len);
979         } else if (!encrypted && config->anonymous_identity) {
980                 identity = config->anonymous_identity;
981                 identity_len = config->anonymous_identity_len;
982                 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity",
983                                   identity, identity_len);
984         } else {
985                 identity = config->identity;
986                 identity_len = config->identity_len;
987                 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity",
988                                   identity, identity_len);
989         }
990
991         if (identity == NULL) {
992                 wpa_printf(MSG_WARNING, "EAP: buildIdentity: identity "
993                            "configuration was not available");
994                 if (config->pcsc) {
995                         if (eap_sm_get_scard_identity(sm, config) < 0)
996                                 return NULL;
997                         identity = config->identity;
998                         identity_len = config->identity_len;
999                         wpa_hexdump_ascii(MSG_DEBUG, "permanent identity from "
1000                                           "IMSI", identity, identity_len);
1001                 } else {
1002                         eap_sm_request_identity(sm);
1003                         return NULL;
1004                 }
1005         } else if (config->pcsc) {
1006                 if (eap_sm_set_scard_pin(sm, config) < 0)
1007                         return NULL;
1008         }
1009
1010         resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, identity_len,
1011                              EAP_CODE_RESPONSE, id);
1012         if (resp == NULL)
1013                 return NULL;
1014
1015         wpabuf_put_data(resp, identity, identity_len);
1016
1017         return resp;
1018 }
1019
1020
1021 static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req)
1022 {
1023         const u8 *pos;
1024         char *msg;
1025         size_t i, msg_len;
1026
1027         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, req,
1028                                &msg_len);
1029         if (pos == NULL)
1030                 return;
1031         wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Notification data",
1032                           pos, msg_len);
1033
1034         msg = os_malloc(msg_len + 1);
1035         if (msg == NULL)
1036                 return;
1037         for (i = 0; i < msg_len; i++)
1038                 msg[i] = isprint(pos[i]) ? (char) pos[i] : '_';
1039         msg[msg_len] = '\0';
1040         wpa_msg(sm->msg_ctx, MSG_INFO, "%s%s",
1041                 WPA_EVENT_EAP_NOTIFICATION, msg);
1042         os_free(msg);
1043 }
1044
1045
1046 static struct wpabuf * eap_sm_buildNotify(int id)
1047 {
1048         struct wpabuf *resp;
1049
1050         wpa_printf(MSG_DEBUG, "EAP: Generating EAP-Response Notification");
1051         resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, 0,
1052                              EAP_CODE_RESPONSE, id);
1053         if (resp == NULL)
1054                 return NULL;
1055
1056         return resp;
1057 }
1058
1059
1060 static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req)
1061 {
1062         const struct eap_hdr *hdr;
1063         size_t plen;
1064         const u8 *pos;
1065
1066         sm->rxReq = sm->rxResp = sm->rxSuccess = sm->rxFailure = FALSE;
1067         sm->reqId = 0;
1068         sm->reqMethod = EAP_TYPE_NONE;
1069         sm->reqVendor = EAP_VENDOR_IETF;
1070         sm->reqVendorMethod = EAP_TYPE_NONE;
1071
1072         if (req == NULL || wpabuf_len(req) < sizeof(*hdr))
1073                 return;
1074
1075         hdr = wpabuf_head(req);
1076         plen = be_to_host16(hdr->length);
1077         if (plen > wpabuf_len(req)) {
1078                 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
1079                            "(len=%lu plen=%lu)",
1080                            (unsigned long) wpabuf_len(req),
1081                            (unsigned long) plen);
1082                 return;
1083         }
1084
1085         sm->reqId = hdr->identifier;
1086
1087         if (sm->workaround) {
1088                 const u8 *addr[1];
1089                 addr[0] = wpabuf_head(req);
1090                 md5_vector(1, addr, &plen, sm->req_md5);
1091         }
1092
1093         switch (hdr->code) {
1094         case EAP_CODE_REQUEST:
1095                 if (plen < sizeof(*hdr) + 1) {
1096                         wpa_printf(MSG_DEBUG, "EAP: Too short EAP-Request - "
1097                                    "no Type field");
1098                         return;
1099                 }
1100                 sm->rxReq = TRUE;
1101                 pos = (const u8 *) (hdr + 1);
1102                 sm->reqMethod = *pos++;
1103                 if (sm->reqMethod == EAP_TYPE_EXPANDED) {
1104                         if (plen < sizeof(*hdr) + 8) {
1105                                 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
1106                                            "expanded EAP-Packet (plen=%lu)",
1107                                            (unsigned long) plen);
1108                                 return;
1109                         }
1110                         sm->reqVendor = WPA_GET_BE24(pos);
1111                         pos += 3;
1112                         sm->reqVendorMethod = WPA_GET_BE32(pos);
1113                 }
1114                 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Request id=%d "
1115                            "method=%u vendor=%u vendorMethod=%u",
1116                            sm->reqId, sm->reqMethod, sm->reqVendor,
1117                            sm->reqVendorMethod);
1118                 break;
1119         case EAP_CODE_RESPONSE:
1120                 if (sm->selectedMethod == EAP_TYPE_LEAP) {
1121                         /*
1122                          * LEAP differs from RFC 4137 by using reversed roles
1123                          * for mutual authentication and because of this, we
1124                          * need to accept EAP-Response frames if LEAP is used.
1125                          */
1126                         if (plen < sizeof(*hdr) + 1) {
1127                                 wpa_printf(MSG_DEBUG, "EAP: Too short "
1128                                            "EAP-Response - no Type field");
1129                                 return;
1130                         }
1131                         sm->rxResp = TRUE;
1132                         pos = (const u8 *) (hdr + 1);
1133                         sm->reqMethod = *pos;
1134                         wpa_printf(MSG_DEBUG, "EAP: Received EAP-Response for "
1135                                    "LEAP method=%d id=%d",
1136                                    sm->reqMethod, sm->reqId);
1137                         break;
1138                 }
1139                 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Response");
1140                 break;
1141         case EAP_CODE_SUCCESS:
1142                 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Success");
1143                 sm->rxSuccess = TRUE;
1144                 break;
1145         case EAP_CODE_FAILURE:
1146                 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Failure");
1147                 sm->rxFailure = TRUE;
1148                 break;
1149         default:
1150                 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Packet with unknown "
1151                            "code %d", hdr->code);
1152                 break;
1153         }
1154 }
1155
1156
1157 /**
1158  * eap_peer_sm_init - Allocate and initialize EAP peer state machine
1159  * @eapol_ctx: Context data to be used with eapol_cb calls
1160  * @eapol_cb: Pointer to EAPOL callback functions
1161  * @msg_ctx: Context data for wpa_msg() calls
1162  * @conf: EAP configuration
1163  * Returns: Pointer to the allocated EAP state machine or %NULL on failure
1164  *
1165  * This function allocates and initializes an EAP state machine. In addition,
1166  * this initializes TLS library for the new EAP state machine. eapol_cb pointer
1167  * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
1168  * state machine. Consequently, the caller must make sure that this data
1169  * structure remains alive while the EAP state machine is active.
1170  */
1171 struct eap_sm * eap_peer_sm_init(void *eapol_ctx,
1172                                  struct eapol_callbacks *eapol_cb,
1173                                  void *msg_ctx, struct eap_config *conf)
1174 {
1175         struct eap_sm *sm;
1176         struct tls_config tlsconf;
1177
1178         sm = os_zalloc(sizeof(*sm));
1179         if (sm == NULL)
1180                 return NULL;
1181         sm->eapol_ctx = eapol_ctx;
1182         sm->eapol_cb = eapol_cb;
1183         sm->msg_ctx = msg_ctx;
1184         sm->ClientTimeout = 60;
1185         sm->wps = conf->wps;
1186
1187         os_memset(&tlsconf, 0, sizeof(tlsconf));
1188         tlsconf.opensc_engine_path = conf->opensc_engine_path;
1189         tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path;
1190         tlsconf.pkcs11_module_path = conf->pkcs11_module_path;
1191 #ifdef CONFIG_FIPS
1192         tlsconf.fips_mode = 1;
1193 #endif /* CONFIG_FIPS */
1194         sm->ssl_ctx = tls_init(&tlsconf);
1195         if (sm->ssl_ctx == NULL) {
1196                 wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS "
1197                            "context.");
1198                 os_free(sm);
1199                 return NULL;
1200         }
1201
1202         return sm;
1203 }
1204
1205
1206 /**
1207  * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
1208  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1209  *
1210  * This function deinitializes EAP state machine and frees all allocated
1211  * resources.
1212  */
1213 void eap_peer_sm_deinit(struct eap_sm *sm)
1214 {
1215         if (sm == NULL)
1216                 return;
1217         eap_deinit_prev_method(sm, "EAP deinit");
1218         eap_sm_abort(sm);
1219         tls_deinit(sm->ssl_ctx);
1220         os_free(sm);
1221 }
1222
1223
1224 /**
1225  * eap_peer_sm_step - Step EAP peer state machine
1226  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1227  * Returns: 1 if EAP state was changed or 0 if not
1228  *
1229  * This function advances EAP state machine to a new state to match with the
1230  * current variables. This should be called whenever variables used by the EAP
1231  * state machine have changed.
1232  */
1233 int eap_peer_sm_step(struct eap_sm *sm)
1234 {
1235         int res = 0;
1236         do {
1237                 sm->changed = FALSE;
1238                 SM_STEP_RUN(EAP);
1239                 if (sm->changed)
1240                         res = 1;
1241         } while (sm->changed);
1242         return res;
1243 }
1244
1245
1246 /**
1247  * eap_sm_abort - Abort EAP authentication
1248  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1249  *
1250  * Release system resources that have been allocated for the authentication
1251  * session without fully deinitializing the EAP state machine.
1252  */
1253 void eap_sm_abort(struct eap_sm *sm)
1254 {
1255         wpabuf_free(sm->lastRespData);
1256         sm->lastRespData = NULL;
1257         wpabuf_free(sm->eapRespData);
1258         sm->eapRespData = NULL;
1259         os_free(sm->eapKeyData);
1260         sm->eapKeyData = NULL;
1261
1262         /* This is not clearly specified in the EAP statemachines draft, but
1263          * it seems necessary to make sure that some of the EAPOL variables get
1264          * cleared for the next authentication. */
1265         eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
1266 }
1267
1268
1269 #ifdef CONFIG_CTRL_IFACE
1270 static const char * eap_sm_state_txt(int state)
1271 {
1272         switch (state) {
1273         case EAP_INITIALIZE:
1274                 return "INITIALIZE";
1275         case EAP_DISABLED:
1276                 return "DISABLED";
1277         case EAP_IDLE:
1278                 return "IDLE";
1279         case EAP_RECEIVED:
1280                 return "RECEIVED";
1281         case EAP_GET_METHOD:
1282                 return "GET_METHOD";
1283         case EAP_METHOD:
1284                 return "METHOD";
1285         case EAP_SEND_RESPONSE:
1286                 return "SEND_RESPONSE";
1287         case EAP_DISCARD:
1288                 return "DISCARD";
1289         case EAP_IDENTITY:
1290                 return "IDENTITY";
1291         case EAP_NOTIFICATION:
1292                 return "NOTIFICATION";
1293         case EAP_RETRANSMIT:
1294                 return "RETRANSMIT";
1295         case EAP_SUCCESS:
1296                 return "SUCCESS";
1297         case EAP_FAILURE:
1298                 return "FAILURE";
1299         default:
1300                 return "UNKNOWN";
1301         }
1302 }
1303 #endif /* CONFIG_CTRL_IFACE */
1304
1305
1306 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1307 static const char * eap_sm_method_state_txt(EapMethodState state)
1308 {
1309         switch (state) {
1310         case METHOD_NONE:
1311                 return "NONE";
1312         case METHOD_INIT:
1313                 return "INIT";
1314         case METHOD_CONT:
1315                 return "CONT";
1316         case METHOD_MAY_CONT:
1317                 return "MAY_CONT";
1318         case METHOD_DONE:
1319                 return "DONE";
1320         default:
1321                 return "UNKNOWN";
1322         }
1323 }
1324
1325
1326 static const char * eap_sm_decision_txt(EapDecision decision)
1327 {
1328         switch (decision) {
1329         case DECISION_FAIL:
1330                 return "FAIL";
1331         case DECISION_COND_SUCC:
1332                 return "COND_SUCC";
1333         case DECISION_UNCOND_SUCC:
1334                 return "UNCOND_SUCC";
1335         default:
1336                 return "UNKNOWN";
1337         }
1338 }
1339 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1340
1341
1342 #ifdef CONFIG_CTRL_IFACE
1343
1344 /**
1345  * eap_sm_get_status - Get EAP state machine status
1346  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1347  * @buf: Buffer for status information
1348  * @buflen: Maximum buffer length
1349  * @verbose: Whether to include verbose status information
1350  * Returns: Number of bytes written to buf.
1351  *
1352  * Query EAP state machine for status information. This function fills in a
1353  * text area with current status information from the EAPOL state machine. If
1354  * the buffer (buf) is not large enough, status information will be truncated
1355  * to fit the buffer.
1356  */
1357 int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose)
1358 {
1359         int len, ret;
1360
1361         if (sm == NULL)
1362                 return 0;
1363
1364         len = os_snprintf(buf, buflen,
1365                           "EAP state=%s\n",
1366                           eap_sm_state_txt(sm->EAP_state));
1367         if (len < 0 || (size_t) len >= buflen)
1368                 return 0;
1369
1370         if (sm->selectedMethod != EAP_TYPE_NONE) {
1371                 const char *name;
1372                 if (sm->m) {
1373                         name = sm->m->name;
1374                 } else {
1375                         const struct eap_method *m =
1376                                 eap_peer_get_eap_method(EAP_VENDOR_IETF,
1377                                                         sm->selectedMethod);
1378                         if (m)
1379                                 name = m->name;
1380                         else
1381                                 name = "?";
1382                 }
1383                 ret = os_snprintf(buf + len, buflen - len,
1384                                   "selectedMethod=%d (EAP-%s)\n",
1385                                   sm->selectedMethod, name);
1386                 if (ret < 0 || (size_t) ret >= buflen - len)
1387                         return len;
1388                 len += ret;
1389
1390                 if (sm->m && sm->m->get_status) {
1391                         len += sm->m->get_status(sm, sm->eap_method_priv,
1392                                                  buf + len, buflen - len,
1393                                                  verbose);
1394                 }
1395         }
1396
1397         if (verbose) {
1398                 ret = os_snprintf(buf + len, buflen - len,
1399                                   "reqMethod=%d\n"
1400                                   "methodState=%s\n"
1401                                   "decision=%s\n"
1402                                   "ClientTimeout=%d\n",
1403                                   sm->reqMethod,
1404                                   eap_sm_method_state_txt(sm->methodState),
1405                                   eap_sm_decision_txt(sm->decision),
1406                                   sm->ClientTimeout);
1407                 if (ret < 0 || (size_t) ret >= buflen - len)
1408                         return len;
1409                 len += ret;
1410         }
1411
1412         return len;
1413 }
1414 #endif /* CONFIG_CTRL_IFACE */
1415
1416
1417 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1418 typedef enum {
1419         TYPE_IDENTITY, TYPE_PASSWORD, TYPE_OTP, TYPE_PIN, TYPE_NEW_PASSWORD,
1420         TYPE_PASSPHRASE
1421 } eap_ctrl_req_type;
1422
1423 static void eap_sm_request(struct eap_sm *sm, eap_ctrl_req_type type,
1424                            const char *msg, size_t msglen)
1425 {
1426         struct eap_peer_config *config;
1427         char *field, *txt, *tmp;
1428
1429         if (sm == NULL)
1430                 return;
1431         config = eap_get_config(sm);
1432         if (config == NULL)
1433                 return;
1434
1435         switch (type) {
1436         case TYPE_IDENTITY:
1437                 field = "IDENTITY";
1438                 txt = "Identity";
1439                 config->pending_req_identity++;
1440                 break;
1441         case TYPE_PASSWORD:
1442                 field = "PASSWORD";
1443                 txt = "Password";
1444                 config->pending_req_password++;
1445                 break;
1446         case TYPE_NEW_PASSWORD:
1447                 field = "NEW_PASSWORD";
1448                 txt = "New Password";
1449                 config->pending_req_new_password++;
1450                 break;
1451         case TYPE_PIN:
1452                 field = "PIN";
1453                 txt = "PIN";
1454                 config->pending_req_pin++;
1455                 break;
1456         case TYPE_OTP:
1457                 field = "OTP";
1458                 if (msg) {
1459                         tmp = os_malloc(msglen + 3);
1460                         if (tmp == NULL)
1461                                 return;
1462                         tmp[0] = '[';
1463                         os_memcpy(tmp + 1, msg, msglen);
1464                         tmp[msglen + 1] = ']';
1465                         tmp[msglen + 2] = '\0';
1466                         txt = tmp;
1467                         os_free(config->pending_req_otp);
1468                         config->pending_req_otp = tmp;
1469                         config->pending_req_otp_len = msglen + 3;
1470                 } else {
1471                         if (config->pending_req_otp == NULL)
1472                                 return;
1473                         txt = config->pending_req_otp;
1474                 }
1475                 break;
1476         case TYPE_PASSPHRASE:
1477                 field = "PASSPHRASE";
1478                 txt = "Private key passphrase";
1479                 config->pending_req_passphrase++;
1480                 break;
1481         default:
1482                 return;
1483         }
1484
1485         if (sm->eapol_cb->eap_param_needed)
1486                 sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt);
1487 }
1488 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1489 #define eap_sm_request(sm, type, msg, msglen) do { } while (0)
1490 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1491
1492
1493 /**
1494  * eap_sm_request_identity - Request identity from user (ctrl_iface)
1495  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1496  *
1497  * EAP methods can call this function to request identity information for the
1498  * current network. This is normally called when the identity is not included
1499  * in the network configuration. The request will be sent to monitor programs
1500  * through the control interface.
1501  */
1502 void eap_sm_request_identity(struct eap_sm *sm)
1503 {
1504         eap_sm_request(sm, TYPE_IDENTITY, NULL, 0);
1505 }
1506
1507
1508 /**
1509  * eap_sm_request_password - Request password from user (ctrl_iface)
1510  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1511  *
1512  * EAP methods can call this function to request password information for the
1513  * current network. This is normally called when the password is not included
1514  * in the network configuration. The request will be sent to monitor programs
1515  * through the control interface.
1516  */
1517 void eap_sm_request_password(struct eap_sm *sm)
1518 {
1519         eap_sm_request(sm, TYPE_PASSWORD, NULL, 0);
1520 }
1521
1522
1523 /**
1524  * eap_sm_request_new_password - Request new password from user (ctrl_iface)
1525  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1526  *
1527  * EAP methods can call this function to request new password information for
1528  * the current network. This is normally called when the EAP method indicates
1529  * that the current password has expired and password change is required. The
1530  * request will be sent to monitor programs through the control interface.
1531  */
1532 void eap_sm_request_new_password(struct eap_sm *sm)
1533 {
1534         eap_sm_request(sm, TYPE_NEW_PASSWORD, NULL, 0);
1535 }
1536
1537
1538 /**
1539  * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
1540  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1541  *
1542  * EAP methods can call this function to request SIM or smart card PIN
1543  * information for the current network. This is normally called when the PIN is
1544  * not included in the network configuration. The request will be sent to
1545  * monitor programs through the control interface.
1546  */
1547 void eap_sm_request_pin(struct eap_sm *sm)
1548 {
1549         eap_sm_request(sm, TYPE_PIN, NULL, 0);
1550 }
1551
1552
1553 /**
1554  * eap_sm_request_otp - Request one time password from user (ctrl_iface)
1555  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1556  * @msg: Message to be displayed to the user when asking for OTP
1557  * @msg_len: Length of the user displayable message
1558  *
1559  * EAP methods can call this function to request open time password (OTP) for
1560  * the current network. The request will be sent to monitor programs through
1561  * the control interface.
1562  */
1563 void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len)
1564 {
1565         eap_sm_request(sm, TYPE_OTP, msg, msg_len);
1566 }
1567
1568
1569 /**
1570  * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
1571  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1572  *
1573  * EAP methods can call this function to request passphrase for a private key
1574  * for the current network. This is normally called when the passphrase is not
1575  * included in the network configuration. The request will be sent to monitor
1576  * programs through the control interface.
1577  */
1578 void eap_sm_request_passphrase(struct eap_sm *sm)
1579 {
1580         eap_sm_request(sm, TYPE_PASSPHRASE, NULL, 0);
1581 }
1582
1583
1584 /**
1585  * eap_sm_notify_ctrl_attached - Notification of attached monitor
1586  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1587  *
1588  * Notify EAP state machines that a monitor was attached to the control
1589  * interface to trigger re-sending of pending requests for user input.
1590  */
1591 void eap_sm_notify_ctrl_attached(struct eap_sm *sm)
1592 {
1593         struct eap_peer_config *config = eap_get_config(sm);
1594
1595         if (config == NULL)
1596                 return;
1597
1598         /* Re-send any pending requests for user data since a new control
1599          * interface was added. This handles cases where the EAP authentication
1600          * starts immediately after system startup when the user interface is
1601          * not yet running. */
1602         if (config->pending_req_identity)
1603                 eap_sm_request_identity(sm);
1604         if (config->pending_req_password)
1605                 eap_sm_request_password(sm);
1606         if (config->pending_req_new_password)
1607                 eap_sm_request_new_password(sm);
1608         if (config->pending_req_otp)
1609                 eap_sm_request_otp(sm, NULL, 0);
1610         if (config->pending_req_pin)
1611                 eap_sm_request_pin(sm);
1612         if (config->pending_req_passphrase)
1613                 eap_sm_request_passphrase(sm);
1614 }
1615
1616
1617 static int eap_allowed_phase2_type(int vendor, int type)
1618 {
1619         if (vendor != EAP_VENDOR_IETF)
1620                 return 0;
1621         return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS &&
1622                 type != EAP_TYPE_FAST;
1623 }
1624
1625
1626 /**
1627  * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
1628  * @name: EAP method name, e.g., MD5
1629  * @vendor: Buffer for returning EAP Vendor-Id
1630  * Returns: EAP method type or %EAP_TYPE_NONE if not found
1631  *
1632  * This function maps EAP type names into EAP type numbers that are allowed for
1633  * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
1634  * EAP-PEAP, EAP-TTLS, and EAP-FAST.
1635  */
1636 u32 eap_get_phase2_type(const char *name, int *vendor)
1637 {
1638         int v;
1639         u8 type = eap_peer_get_type(name, &v);
1640         if (eap_allowed_phase2_type(v, type)) {
1641                 *vendor = v;
1642                 return type;
1643         }
1644         *vendor = EAP_VENDOR_IETF;
1645         return EAP_TYPE_NONE;
1646 }
1647
1648
1649 /**
1650  * eap_get_phase2_types - Get list of allowed EAP phase 2 types
1651  * @config: Pointer to a network configuration
1652  * @count: Pointer to a variable to be filled with number of returned EAP types
1653  * Returns: Pointer to allocated type list or %NULL on failure
1654  *
1655  * This function generates an array of allowed EAP phase 2 (tunneled) types for
1656  * the given network configuration.
1657  */
1658 struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config,
1659                                               size_t *count)
1660 {
1661         struct eap_method_type *buf;
1662         u32 method;
1663         int vendor;
1664         size_t mcount;
1665         const struct eap_method *methods, *m;
1666
1667         methods = eap_peer_get_methods(&mcount);
1668         if (methods == NULL)
1669                 return NULL;
1670         *count = 0;
1671         buf = os_malloc(mcount * sizeof(struct eap_method_type));
1672         if (buf == NULL)
1673                 return NULL;
1674
1675         for (m = methods; m; m = m->next) {
1676                 vendor = m->vendor;
1677                 method = m->method;
1678                 if (eap_allowed_phase2_type(vendor, method)) {
1679                         if (vendor == EAP_VENDOR_IETF &&
1680                             method == EAP_TYPE_TLS && config &&
1681                             config->private_key2 == NULL)
1682                                 continue;
1683                         buf[*count].vendor = vendor;
1684                         buf[*count].method = method;
1685                         (*count)++;
1686                 }
1687         }
1688
1689         return buf;
1690 }
1691
1692
1693 /**
1694  * eap_set_fast_reauth - Update fast_reauth setting
1695  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1696  * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
1697  */
1698 void eap_set_fast_reauth(struct eap_sm *sm, int enabled)
1699 {
1700         sm->fast_reauth = enabled;
1701 }
1702
1703
1704 /**
1705  * eap_set_workaround - Update EAP workarounds setting
1706  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1707  * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
1708  */
1709 void eap_set_workaround(struct eap_sm *sm, unsigned int workaround)
1710 {
1711         sm->workaround = workaround;
1712 }
1713
1714
1715 /**
1716  * eap_get_config - Get current network configuration
1717  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1718  * Returns: Pointer to the current network configuration or %NULL if not found
1719  *
1720  * EAP peer methods should avoid using this function if they can use other
1721  * access functions, like eap_get_config_identity() and
1722  * eap_get_config_password(), that do not require direct access to
1723  * struct eap_peer_config.
1724  */
1725 struct eap_peer_config * eap_get_config(struct eap_sm *sm)
1726 {
1727         return sm->eapol_cb->get_config(sm->eapol_ctx);
1728 }
1729
1730
1731 /**
1732  * eap_get_config_identity - Get identity from the network configuration
1733  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1734  * @len: Buffer for the length of the identity
1735  * Returns: Pointer to the identity or %NULL if not found
1736  */
1737 const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
1738 {
1739         struct eap_peer_config *config = eap_get_config(sm);
1740         if (config == NULL)
1741                 return NULL;
1742         *len = config->identity_len;
1743         return config->identity;
1744 }
1745
1746
1747 /**
1748  * eap_get_config_password - Get password from the network configuration
1749  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1750  * @len: Buffer for the length of the password
1751  * Returns: Pointer to the password or %NULL if not found
1752  */
1753 const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
1754 {
1755         struct eap_peer_config *config = eap_get_config(sm);
1756         if (config == NULL)
1757                 return NULL;
1758         *len = config->password_len;
1759         return config->password;
1760 }
1761
1762
1763 /**
1764  * eap_get_config_password2 - Get password from the network configuration
1765  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1766  * @len: Buffer for the length of the password
1767  * @hash: Buffer for returning whether the password is stored as a
1768  * NtPasswordHash instead of plaintext password; can be %NULL if this
1769  * information is not needed
1770  * Returns: Pointer to the password or %NULL if not found
1771  */
1772 const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
1773 {
1774         struct eap_peer_config *config = eap_get_config(sm);
1775         if (config == NULL)
1776                 return NULL;
1777         *len = config->password_len;
1778         if (hash)
1779                 *hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH);
1780         return config->password;
1781 }
1782
1783
1784 /**
1785  * eap_get_config_new_password - Get new password from network configuration
1786  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1787  * @len: Buffer for the length of the new password
1788  * Returns: Pointer to the new password or %NULL if not found
1789  */
1790 const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
1791 {
1792         struct eap_peer_config *config = eap_get_config(sm);
1793         if (config == NULL)
1794                 return NULL;
1795         *len = config->new_password_len;
1796         return config->new_password;
1797 }
1798
1799
1800 /**
1801  * eap_get_config_otp - Get one-time password from the network configuration
1802  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1803  * @len: Buffer for the length of the one-time password
1804  * Returns: Pointer to the one-time password or %NULL if not found
1805  */
1806 const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len)
1807 {
1808         struct eap_peer_config *config = eap_get_config(sm);
1809         if (config == NULL)
1810                 return NULL;
1811         *len = config->otp_len;
1812         return config->otp;
1813 }
1814
1815
1816 /**
1817  * eap_clear_config_otp - Clear used one-time password
1818  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1819  *
1820  * This function clears a used one-time password (OTP) from the current network
1821  * configuration. This should be called when the OTP has been used and is not
1822  * needed anymore.
1823  */
1824 void eap_clear_config_otp(struct eap_sm *sm)
1825 {
1826         struct eap_peer_config *config = eap_get_config(sm);
1827         if (config == NULL)
1828                 return;
1829         os_memset(config->otp, 0, config->otp_len);
1830         os_free(config->otp);
1831         config->otp = NULL;
1832         config->otp_len = 0;
1833 }
1834
1835
1836 /**
1837  * eap_get_config_phase1 - Get phase1 data from the network configuration
1838  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1839  * Returns: Pointer to the phase1 data or %NULL if not found
1840  */
1841 const char * eap_get_config_phase1(struct eap_sm *sm)
1842 {
1843         struct eap_peer_config *config = eap_get_config(sm);
1844         if (config == NULL)
1845                 return NULL;
1846         return config->phase1;
1847 }
1848
1849
1850 /**
1851  * eap_get_config_phase2 - Get phase2 data from the network configuration
1852  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1853  * Returns: Pointer to the phase1 data or %NULL if not found
1854  */
1855 const char * eap_get_config_phase2(struct eap_sm *sm)
1856 {
1857         struct eap_peer_config *config = eap_get_config(sm);
1858         if (config == NULL)
1859                 return NULL;
1860         return config->phase2;
1861 }
1862
1863
1864 /**
1865  * eap_key_available - Get key availability (eapKeyAvailable variable)
1866  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1867  * Returns: 1 if EAP keying material is available, 0 if not
1868  */
1869 int eap_key_available(struct eap_sm *sm)
1870 {
1871         return sm ? sm->eapKeyAvailable : 0;
1872 }
1873
1874
1875 /**
1876  * eap_notify_success - Notify EAP state machine about external success trigger
1877  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1878  *
1879  * This function is called when external event, e.g., successful completion of
1880  * WPA-PSK key handshake, is indicating that EAP state machine should move to
1881  * success state. This is mainly used with security modes that do not use EAP
1882  * state machine (e.g., WPA-PSK).
1883  */
1884 void eap_notify_success(struct eap_sm *sm)
1885 {
1886         if (sm) {
1887                 sm->decision = DECISION_COND_SUCC;
1888                 sm->EAP_state = EAP_SUCCESS;
1889         }
1890 }
1891
1892
1893 /**
1894  * eap_notify_lower_layer_success - Notification of lower layer success
1895  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1896  *
1897  * Notify EAP state machines that a lower layer has detected a successful
1898  * authentication. This is used to recover from dropped EAP-Success messages.
1899  */
1900 void eap_notify_lower_layer_success(struct eap_sm *sm)
1901 {
1902         if (sm == NULL)
1903                 return;
1904
1905         if (eapol_get_bool(sm, EAPOL_eapSuccess) ||
1906             sm->decision == DECISION_FAIL ||
1907             (sm->methodState != METHOD_MAY_CONT &&
1908              sm->methodState != METHOD_DONE))
1909                 return;
1910
1911         if (sm->eapKeyData != NULL)
1912                 sm->eapKeyAvailable = TRUE;
1913         eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
1914         wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
1915                 "EAP authentication completed successfully (based on lower "
1916                 "layer success)");
1917 }
1918
1919
1920 /**
1921  * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
1922  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1923  * @len: Pointer to variable that will be set to number of bytes in the key
1924  * Returns: Pointer to the EAP keying data or %NULL on failure
1925  *
1926  * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
1927  * key is available only after a successful authentication. EAP state machine
1928  * continues to manage the key data and the caller must not change or free the
1929  * returned data.
1930  */
1931 const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len)
1932 {
1933         if (sm == NULL || sm->eapKeyData == NULL) {
1934                 *len = 0;
1935                 return NULL;
1936         }
1937
1938         *len = sm->eapKeyDataLen;
1939         return sm->eapKeyData;
1940 }
1941
1942
1943 /**
1944  * eap_get_eapKeyData - Get EAP response data
1945  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1946  * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
1947  *
1948  * Fetch EAP response (eapRespData) from the EAP state machine. This data is
1949  * available when EAP state machine has processed an incoming EAP request. The
1950  * EAP state machine does not maintain a reference to the response after this
1951  * function is called and the caller is responsible for freeing the data.
1952  */
1953 struct wpabuf * eap_get_eapRespData(struct eap_sm *sm)
1954 {
1955         struct wpabuf *resp;
1956
1957         if (sm == NULL || sm->eapRespData == NULL)
1958                 return NULL;
1959
1960         resp = sm->eapRespData;
1961         sm->eapRespData = NULL;
1962
1963         return resp;
1964 }
1965
1966
1967 /**
1968  * eap_sm_register_scard_ctx - Notification of smart card context
1969  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1970  * @ctx: Context data for smart card operations
1971  *
1972  * Notify EAP state machines of context data for smart card operations. This
1973  * context data will be used as a parameter for scard_*() functions.
1974  */
1975 void eap_register_scard_ctx(struct eap_sm *sm, void *ctx)
1976 {
1977         if (sm)
1978                 sm->scard_ctx = ctx;
1979 }
1980
1981
1982 /**
1983  * eap_set_config_blob - Set or add a named configuration blob
1984  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1985  * @blob: New value for the blob
1986  *
1987  * Adds a new configuration blob or replaces the current value of an existing
1988  * blob.
1989  */
1990 void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob)
1991 {
1992 #ifndef CONFIG_NO_CONFIG_BLOBS
1993         sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob);
1994 #endif /* CONFIG_NO_CONFIG_BLOBS */
1995 }
1996
1997
1998 /**
1999  * eap_get_config_blob - Get a named configuration blob
2000  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2001  * @name: Name of the blob
2002  * Returns: Pointer to blob data or %NULL if not found
2003  */
2004 const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm,
2005                                                    const char *name)
2006 {
2007 #ifndef CONFIG_NO_CONFIG_BLOBS
2008         return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name);
2009 #else /* CONFIG_NO_CONFIG_BLOBS */
2010         return NULL;
2011 #endif /* CONFIG_NO_CONFIG_BLOBS */
2012 }
2013
2014
2015 /**
2016  * eap_set_force_disabled - Set force_disabled flag
2017  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2018  * @disabled: 1 = EAP disabled, 0 = EAP enabled
2019  *
2020  * This function is used to force EAP state machine to be disabled when it is
2021  * not in use (e.g., with WPA-PSK or plaintext connections).
2022  */
2023 void eap_set_force_disabled(struct eap_sm *sm, int disabled)
2024 {
2025         sm->force_disabled = disabled;
2026 }
2027
2028
2029  /**
2030  * eap_notify_pending - Notify that EAP method is ready to re-process a request
2031  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2032  *
2033  * An EAP method can perform a pending operation (e.g., to get a response from
2034  * an external process). Once the response is available, this function can be
2035  * used to request EAPOL state machine to retry delivering the previously
2036  * received (and still unanswered) EAP request to EAP state machine.
2037  */
2038 void eap_notify_pending(struct eap_sm *sm)
2039 {
2040         sm->eapol_cb->notify_pending(sm->eapol_ctx);
2041 }
2042
2043
2044 /**
2045  * eap_invalidate_cached_session - Mark cached session data invalid
2046  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2047  */
2048 void eap_invalidate_cached_session(struct eap_sm *sm)
2049 {
2050         if (sm)
2051                 eap_deinit_prev_method(sm, "invalidate");
2052 }
2053
2054
2055 int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf)
2056 {
2057         if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2058             os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2059                 return 0; /* Not a WPS Enrollee */
2060
2061         if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL)
2062                 return 0; /* Not using PBC */
2063
2064         return 1;
2065 }
2066
2067
2068 int eap_is_wps_pin_enrollee(struct eap_peer_config *conf)
2069 {
2070         if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2071             os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2072                 return 0; /* Not a WPS Enrollee */
2073
2074         if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL)
2075                 return 0; /* Not using PIN */
2076
2077         return 1;
2078 }