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