blob: 98abd4ef7d21c6eb81e047cdf50a0bf0f6c3f9b9 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * EAP peer state machines (RFC 4137)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
8 * This file implements the Peer State Machine as defined in RFC 4137. The used
9 * states and state transitions match mostly with the RFC. However, there are
10 * couple of additional transitions for working around small issues noticed
11 * during testing. These exceptions are explained in comments within the
12 * functions in this file. The method functions, m.func(), are similar to the
13 * ones used in RFC 4137, but some small changes have used here to optimize
14 * operations and to add functionality needed for fast re-authentication
15 * (session resumption).
16 */
17
18#include "includes.h"
19
20#include "common.h"
21#include "pcsc_funcs.h"
22#include "state_machine.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070023#include "ext_password.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "crypto/crypto.h"
25#include "crypto/tls.h"
26#include "common/wpa_ctrl.h"
27#include "eap_common/eap_wsc_common.h"
28#include "eap_i.h"
29#include "eap_config.h"
30
31#define STATE_MACHINE_DATA struct eap_sm
32#define STATE_MACHINE_DEBUG_PREFIX "EAP"
33
34#define EAP_MAX_AUTH_ROUNDS 50
35#define EAP_CLIENT_TIMEOUT_DEFAULT 60
36
37
38static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
39 EapType method);
40static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id);
41static void eap_sm_processIdentity(struct eap_sm *sm,
42 const struct wpabuf *req);
43static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req);
44static struct wpabuf * eap_sm_buildNotify(int id);
45static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req);
46#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
47static const char * eap_sm_method_state_txt(EapMethodState state);
48static const char * eap_sm_decision_txt(EapDecision decision);
49#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
50
51
52
53static Boolean eapol_get_bool(struct eap_sm *sm, enum eapol_bool_var var)
54{
55 return sm->eapol_cb->get_bool(sm->eapol_ctx, var);
56}
57
58
59static void eapol_set_bool(struct eap_sm *sm, enum eapol_bool_var var,
60 Boolean value)
61{
62 sm->eapol_cb->set_bool(sm->eapol_ctx, var, value);
63}
64
65
66static unsigned int eapol_get_int(struct eap_sm *sm, enum eapol_int_var var)
67{
68 return sm->eapol_cb->get_int(sm->eapol_ctx, var);
69}
70
71
72static void eapol_set_int(struct eap_sm *sm, enum eapol_int_var var,
73 unsigned int value)
74{
75 sm->eapol_cb->set_int(sm->eapol_ctx, var, value);
76}
77
78
79static struct wpabuf * eapol_get_eapReqData(struct eap_sm *sm)
80{
81 return sm->eapol_cb->get_eapReqData(sm->eapol_ctx);
82}
83
84
Dmitry Shmidt04949592012-07-19 12:16:46 -070085static void eap_notify_status(struct eap_sm *sm, const char *status,
86 const char *parameter)
87{
88 wpa_printf(MSG_DEBUG, "EAP: Status notification: %s (param=%s)",
89 status, parameter);
90 if (sm->eapol_cb->notify_status)
91 sm->eapol_cb->notify_status(sm->eapol_ctx, status, parameter);
92}
93
94
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095static void eap_deinit_prev_method(struct eap_sm *sm, const char *txt)
96{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070097 ext_password_free(sm->ext_pw_buf);
98 sm->ext_pw_buf = NULL;
99
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 if (sm->m == NULL || sm->eap_method_priv == NULL)
101 return;
102
103 wpa_printf(MSG_DEBUG, "EAP: deinitialize previously used EAP method "
104 "(%d, %s) at %s", sm->selectedMethod, sm->m->name, txt);
105 sm->m->deinit(sm, sm->eap_method_priv);
106 sm->eap_method_priv = NULL;
107 sm->m = NULL;
108}
109
110
111/**
112 * eap_allowed_method - Check whether EAP method is allowed
113 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
114 * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
115 * @method: EAP type
116 * Returns: 1 = allowed EAP method, 0 = not allowed
117 */
118int eap_allowed_method(struct eap_sm *sm, int vendor, u32 method)
119{
120 struct eap_peer_config *config = eap_get_config(sm);
121 int i;
122 struct eap_method_type *m;
123
124 if (config == NULL || config->eap_methods == NULL)
125 return 1;
126
127 m = config->eap_methods;
128 for (i = 0; m[i].vendor != EAP_VENDOR_IETF ||
129 m[i].method != EAP_TYPE_NONE; i++) {
130 if (m[i].vendor == vendor && m[i].method == method)
131 return 1;
132 }
133 return 0;
134}
135
136
137/*
138 * This state initializes state machine variables when the machine is
139 * activated (portEnabled = TRUE). This is also used when re-starting
140 * authentication (eapRestart == TRUE).
141 */
142SM_STATE(EAP, INITIALIZE)
143{
144 SM_ENTRY(EAP, INITIALIZE);
145 if (sm->fast_reauth && sm->m && sm->m->has_reauth_data &&
146 sm->m->has_reauth_data(sm, sm->eap_method_priv) &&
147 !sm->prev_failure) {
148 wpa_printf(MSG_DEBUG, "EAP: maintaining EAP method data for "
149 "fast reauthentication");
150 sm->m->deinit_for_reauth(sm, sm->eap_method_priv);
151 } else {
152 eap_deinit_prev_method(sm, "INITIALIZE");
153 }
154 sm->selectedMethod = EAP_TYPE_NONE;
155 sm->methodState = METHOD_NONE;
156 sm->allowNotifications = TRUE;
157 sm->decision = DECISION_FAIL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800158 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
160 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
161 eapol_set_bool(sm, EAPOL_eapFail, FALSE);
162 os_free(sm->eapKeyData);
163 sm->eapKeyData = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800164 os_free(sm->eapSessionId);
165 sm->eapSessionId = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166 sm->eapKeyAvailable = FALSE;
167 eapol_set_bool(sm, EAPOL_eapRestart, FALSE);
168 sm->lastId = -1; /* new session - make sure this does not match with
169 * the first EAP-Packet */
170 /*
171 * RFC 4137 does not reset eapResp and eapNoResp here. However, this
172 * seemed to be able to trigger cases where both were set and if EAPOL
173 * state machine uses eapNoResp first, it may end up not sending a real
174 * reply correctly. This occurred when the workaround in FAIL state set
175 * eapNoResp = TRUE.. Maybe that workaround needs to be fixed to do
176 * something else(?)
177 */
178 eapol_set_bool(sm, EAPOL_eapResp, FALSE);
179 eapol_set_bool(sm, EAPOL_eapNoResp, FALSE);
180 sm->num_rounds = 0;
181 sm->prev_failure = 0;
182}
183
184
185/*
186 * This state is reached whenever service from the lower layer is interrupted
187 * or unavailable (portEnabled == FALSE). Immediate transition to INITIALIZE
188 * occurs when the port becomes enabled.
189 */
190SM_STATE(EAP, DISABLED)
191{
192 SM_ENTRY(EAP, DISABLED);
193 sm->num_rounds = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700194 /*
195 * RFC 4137 does not describe clearing of idleWhile here, but doing so
196 * allows the timer tick to be stopped more quickly when EAP is not in
197 * use.
198 */
199 eapol_set_int(sm, EAPOL_idleWhile, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200}
201
202
203/*
204 * The state machine spends most of its time here, waiting for something to
205 * happen. This state is entered unconditionally from INITIALIZE, DISCARD, and
206 * SEND_RESPONSE states.
207 */
208SM_STATE(EAP, IDLE)
209{
210 SM_ENTRY(EAP, IDLE);
211}
212
213
214/*
215 * This state is entered when an EAP packet is received (eapReq == TRUE) to
216 * parse the packet header.
217 */
218SM_STATE(EAP, RECEIVED)
219{
220 const struct wpabuf *eapReqData;
221
222 SM_ENTRY(EAP, RECEIVED);
223 eapReqData = eapol_get_eapReqData(sm);
224 /* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */
225 eap_sm_parseEapReq(sm, eapReqData);
226 sm->num_rounds++;
227}
228
229
230/*
231 * This state is entered when a request for a new type comes in. Either the
232 * correct method is started, or a Nak response is built.
233 */
234SM_STATE(EAP, GET_METHOD)
235{
236 int reinit;
237 EapType method;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700238 const struct eap_method *eap_method;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239
240 SM_ENTRY(EAP, GET_METHOD);
241
242 if (sm->reqMethod == EAP_TYPE_EXPANDED)
243 method = sm->reqVendorMethod;
244 else
245 method = sm->reqMethod;
246
Dmitry Shmidt04949592012-07-19 12:16:46 -0700247 eap_method = eap_peer_get_eap_method(sm->reqVendor, method);
248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249 if (!eap_sm_allowMethod(sm, sm->reqVendor, method)) {
250 wpa_printf(MSG_DEBUG, "EAP: vendor %u method %u not allowed",
251 sm->reqVendor, method);
252 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
253 "vendor=%u method=%u -> NAK",
254 sm->reqVendor, method);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700255 eap_notify_status(sm, "refuse proposed method",
256 eap_method ? eap_method->name : "unknown");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257 goto nak;
258 }
259
260 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
261 "vendor=%u method=%u", sm->reqVendor, method);
262
Dmitry Shmidt04949592012-07-19 12:16:46 -0700263 eap_notify_status(sm, "accept proposed method",
264 eap_method ? eap_method->name : "unknown");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 /*
266 * RFC 4137 does not define specific operation for fast
267 * re-authentication (session resumption). The design here is to allow
268 * the previously used method data to be maintained for
269 * re-authentication if the method support session resumption.
270 * Otherwise, the previously used method data is freed and a new method
271 * is allocated here.
272 */
273 if (sm->fast_reauth &&
274 sm->m && sm->m->vendor == sm->reqVendor &&
275 sm->m->method == method &&
276 sm->m->has_reauth_data &&
277 sm->m->has_reauth_data(sm, sm->eap_method_priv)) {
278 wpa_printf(MSG_DEBUG, "EAP: Using previous method data"
279 " for fast re-authentication");
280 reinit = 1;
281 } else {
282 eap_deinit_prev_method(sm, "GET_METHOD");
283 reinit = 0;
284 }
285
286 sm->selectedMethod = sm->reqMethod;
287 if (sm->m == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700288 sm->m = eap_method;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (!sm->m) {
290 wpa_printf(MSG_DEBUG, "EAP: Could not find selected method: "
291 "vendor %d method %d",
292 sm->reqVendor, method);
293 goto nak;
294 }
295
296 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
297
298 wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP method: "
299 "vendor %u method %u (%s)",
300 sm->reqVendor, method, sm->m->name);
301 if (reinit)
302 sm->eap_method_priv = sm->m->init_for_reauth(
303 sm, sm->eap_method_priv);
304 else
305 sm->eap_method_priv = sm->m->init(sm);
306
307 if (sm->eap_method_priv == NULL) {
308 struct eap_peer_config *config = eap_get_config(sm);
309 wpa_msg(sm->msg_ctx, MSG_INFO,
310 "EAP: Failed to initialize EAP method: vendor %u "
311 "method %u (%s)",
312 sm->reqVendor, method, sm->m->name);
313 sm->m = NULL;
314 sm->methodState = METHOD_NONE;
315 sm->selectedMethod = EAP_TYPE_NONE;
316 if (sm->reqMethod == EAP_TYPE_TLS && config &&
317 (config->pending_req_pin ||
318 config->pending_req_passphrase)) {
319 /*
320 * Return without generating Nak in order to allow
321 * entering of PIN code or passphrase to retry the
322 * current EAP packet.
323 */
324 wpa_printf(MSG_DEBUG, "EAP: Pending PIN/passphrase "
325 "request - skip Nak");
326 return;
327 }
328
329 goto nak;
330 }
331
332 sm->methodState = METHOD_INIT;
333 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_METHOD
334 "EAP vendor %u method %u (%s) selected",
335 sm->reqVendor, method, sm->m->name);
336 return;
337
338nak:
339 wpabuf_free(sm->eapRespData);
340 sm->eapRespData = NULL;
341 sm->eapRespData = eap_sm_buildNak(sm, sm->reqId);
342}
343
344
345/*
346 * The method processing happens here. The request from the authenticator is
347 * processed, and an appropriate response packet is built.
348 */
349SM_STATE(EAP, METHOD)
350{
351 struct wpabuf *eapReqData;
352 struct eap_method_ret ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800353 int min_len = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354
355 SM_ENTRY(EAP, METHOD);
356 if (sm->m == NULL) {
357 wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected");
358 return;
359 }
360
361 eapReqData = eapol_get_eapReqData(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800362 if (sm->m->vendor == EAP_VENDOR_IETF && sm->m->method == EAP_TYPE_LEAP)
363 min_len = 0; /* LEAP uses EAP-Success without payload */
364 if (!eap_hdr_len_valid(eapReqData, min_len))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700365 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366
367 /*
368 * Get ignore, methodState, decision, allowNotifications, and
369 * eapRespData. RFC 4137 uses three separate method procedure (check,
370 * process, and buildResp) in this state. These have been combined into
371 * a single function call to m->process() in order to optimize EAP
372 * method implementation interface a bit. These procedures are only
373 * used from within this METHOD state, so there is no need to keep
374 * these as separate C functions.
375 *
376 * The RFC 4137 procedures return values as follows:
377 * ignore = m.check(eapReqData)
378 * (methodState, decision, allowNotifications) = m.process(eapReqData)
379 * eapRespData = m.buildResp(reqId)
380 */
381 os_memset(&ret, 0, sizeof(ret));
382 ret.ignore = sm->ignore;
383 ret.methodState = sm->methodState;
384 ret.decision = sm->decision;
385 ret.allowNotifications = sm->allowNotifications;
386 wpabuf_free(sm->eapRespData);
387 sm->eapRespData = NULL;
388 sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret,
389 eapReqData);
390 wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s "
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800391 "methodState=%s decision=%s eapRespData=%p",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 ret.ignore ? "TRUE" : "FALSE",
393 eap_sm_method_state_txt(ret.methodState),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800394 eap_sm_decision_txt(ret.decision),
395 sm->eapRespData);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396
397 sm->ignore = ret.ignore;
398 if (sm->ignore)
399 return;
400 sm->methodState = ret.methodState;
401 sm->decision = ret.decision;
402 sm->allowNotifications = ret.allowNotifications;
403
404 if (sm->m->isKeyAvailable && sm->m->getKey &&
405 sm->m->isKeyAvailable(sm, sm->eap_method_priv)) {
406 os_free(sm->eapKeyData);
407 sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv,
408 &sm->eapKeyDataLen);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800409 os_free(sm->eapSessionId);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700410 sm->eapSessionId = NULL;
411 if (sm->m->getSessionId) {
412 sm->eapSessionId = sm->m->getSessionId(
413 sm, sm->eap_method_priv,
414 &sm->eapSessionIdLen);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800415 wpa_hexdump(MSG_DEBUG, "EAP: Session-Id",
416 sm->eapSessionId, sm->eapSessionIdLen);
417 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418 }
419}
420
421
422/*
423 * This state signals the lower layer that a response packet is ready to be
424 * sent.
425 */
426SM_STATE(EAP, SEND_RESPONSE)
427{
428 SM_ENTRY(EAP, SEND_RESPONSE);
429 wpabuf_free(sm->lastRespData);
430 if (sm->eapRespData) {
431 if (sm->workaround)
432 os_memcpy(sm->last_md5, sm->req_md5, 16);
433 sm->lastId = sm->reqId;
434 sm->lastRespData = wpabuf_dup(sm->eapRespData);
435 eapol_set_bool(sm, EAPOL_eapResp, TRUE);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800436 } else {
437 wpa_printf(MSG_DEBUG, "EAP: No eapRespData available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438 sm->lastRespData = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800439 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
441 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
442}
443
444
445/*
446 * This state signals the lower layer that the request was discarded, and no
447 * response packet will be sent at this time.
448 */
449SM_STATE(EAP, DISCARD)
450{
451 SM_ENTRY(EAP, DISCARD);
452 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
453 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
454}
455
456
457/*
458 * Handles requests for Identity method and builds a response.
459 */
460SM_STATE(EAP, IDENTITY)
461{
462 const struct wpabuf *eapReqData;
463
464 SM_ENTRY(EAP, IDENTITY);
465 eapReqData = eapol_get_eapReqData(sm);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700466 if (!eap_hdr_len_valid(eapReqData, 1))
467 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468 eap_sm_processIdentity(sm, eapReqData);
469 wpabuf_free(sm->eapRespData);
470 sm->eapRespData = NULL;
471 sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId, 0);
472}
473
474
475/*
476 * Handles requests for Notification method and builds a response.
477 */
478SM_STATE(EAP, NOTIFICATION)
479{
480 const struct wpabuf *eapReqData;
481
482 SM_ENTRY(EAP, NOTIFICATION);
483 eapReqData = eapol_get_eapReqData(sm);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700484 if (!eap_hdr_len_valid(eapReqData, 1))
485 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 eap_sm_processNotify(sm, eapReqData);
487 wpabuf_free(sm->eapRespData);
488 sm->eapRespData = NULL;
489 sm->eapRespData = eap_sm_buildNotify(sm->reqId);
490}
491
492
493/*
494 * This state retransmits the previous response packet.
495 */
496SM_STATE(EAP, RETRANSMIT)
497{
498 SM_ENTRY(EAP, RETRANSMIT);
499 wpabuf_free(sm->eapRespData);
500 if (sm->lastRespData)
501 sm->eapRespData = wpabuf_dup(sm->lastRespData);
502 else
503 sm->eapRespData = NULL;
504}
505
506
507/*
508 * This state is entered in case of a successful completion of authentication
509 * and state machine waits here until port is disabled or EAP authentication is
510 * restarted.
511 */
512SM_STATE(EAP, SUCCESS)
513{
514 SM_ENTRY(EAP, SUCCESS);
515 if (sm->eapKeyData != NULL)
516 sm->eapKeyAvailable = TRUE;
517 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
518
519 /*
520 * RFC 4137 does not clear eapReq here, but this seems to be required
521 * to avoid processing the same request twice when state machine is
522 * initialized.
523 */
524 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
525
526 /*
527 * RFC 4137 does not set eapNoResp here, but this seems to be required
528 * to get EAPOL Supplicant backend state machine into SUCCESS state. In
529 * addition, either eapResp or eapNoResp is required to be set after
530 * processing the received EAP frame.
531 */
532 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
533
534 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
535 "EAP authentication completed successfully");
536}
537
538
539/*
540 * This state is entered in case of a failure and state machine waits here
541 * until port is disabled or EAP authentication is restarted.
542 */
543SM_STATE(EAP, FAILURE)
544{
545 SM_ENTRY(EAP, FAILURE);
546 eapol_set_bool(sm, EAPOL_eapFail, TRUE);
547
548 /*
549 * RFC 4137 does not clear eapReq here, but this seems to be required
550 * to avoid processing the same request twice when state machine is
551 * initialized.
552 */
553 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
554
555 /*
556 * RFC 4137 does not set eapNoResp here. However, either eapResp or
557 * eapNoResp is required to be set after processing the received EAP
558 * frame.
559 */
560 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
561
562 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
563 "EAP authentication failed");
564
565 sm->prev_failure = 1;
566}
567
568
569static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId)
570{
571 /*
572 * At least Microsoft IAS and Meetinghouse Aegis seem to be sending
573 * EAP-Success/Failure with lastId + 1 even though RFC 3748 and
574 * RFC 4137 require that reqId == lastId. In addition, it looks like
575 * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
576 *
577 * Accept this kind of Id if EAP workarounds are enabled. These are
578 * unauthenticated plaintext messages, so this should have minimal
579 * security implications (bit easier to fake EAP-Success/Failure).
580 */
581 if (sm->workaround && (reqId == ((lastId + 1) & 0xff) ||
582 reqId == ((lastId + 2) & 0xff))) {
583 wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected "
584 "identifier field in EAP Success: "
585 "reqId=%d lastId=%d (these are supposed to be "
586 "same)", reqId, lastId);
587 return 1;
588 }
589 wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d "
590 "lastId=%d", reqId, lastId);
591 return 0;
592}
593
594
595/*
596 * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
597 */
598
599static void eap_peer_sm_step_idle(struct eap_sm *sm)
600{
601 /*
602 * The first three transitions are from RFC 4137. The last two are
603 * local additions to handle special cases with LEAP and PEAP server
604 * not sending EAP-Success in some cases.
605 */
606 if (eapol_get_bool(sm, EAPOL_eapReq))
607 SM_ENTER(EAP, RECEIVED);
608 else if ((eapol_get_bool(sm, EAPOL_altAccept) &&
609 sm->decision != DECISION_FAIL) ||
610 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
611 sm->decision == DECISION_UNCOND_SUCC))
612 SM_ENTER(EAP, SUCCESS);
613 else if (eapol_get_bool(sm, EAPOL_altReject) ||
614 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
615 sm->decision != DECISION_UNCOND_SUCC) ||
616 (eapol_get_bool(sm, EAPOL_altAccept) &&
617 sm->methodState != METHOD_CONT &&
618 sm->decision == DECISION_FAIL))
619 SM_ENTER(EAP, FAILURE);
620 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
621 sm->leap_done && sm->decision != DECISION_FAIL &&
622 sm->methodState == METHOD_DONE)
623 SM_ENTER(EAP, SUCCESS);
624 else if (sm->selectedMethod == EAP_TYPE_PEAP &&
625 sm->peap_done && sm->decision != DECISION_FAIL &&
626 sm->methodState == METHOD_DONE)
627 SM_ENTER(EAP, SUCCESS);
628}
629
630
631static int eap_peer_req_is_duplicate(struct eap_sm *sm)
632{
633 int duplicate;
634
635 duplicate = (sm->reqId == sm->lastId) && sm->rxReq;
636 if (sm->workaround && duplicate &&
637 os_memcmp(sm->req_md5, sm->last_md5, 16) != 0) {
638 /*
639 * RFC 4137 uses (reqId == lastId) as the only verification for
640 * duplicate EAP requests. However, this misses cases where the
641 * AS is incorrectly using the same id again; and
642 * unfortunately, such implementations exist. Use MD5 hash as
643 * an extra verification for the packets being duplicate to
644 * workaround these issues.
645 */
646 wpa_printf(MSG_DEBUG, "EAP: AS used the same Id again, but "
647 "EAP packets were not identical");
648 wpa_printf(MSG_DEBUG, "EAP: workaround - assume this is not a "
649 "duplicate packet");
650 duplicate = 0;
651 }
652
653 return duplicate;
654}
655
656
657static void eap_peer_sm_step_received(struct eap_sm *sm)
658{
659 int duplicate = eap_peer_req_is_duplicate(sm);
660
661 /*
662 * Two special cases below for LEAP are local additions to work around
663 * odd LEAP behavior (EAP-Success in the middle of authentication and
664 * then swapped roles). Other transitions are based on RFC 4137.
665 */
666 if (sm->rxSuccess && sm->decision != DECISION_FAIL &&
667 (sm->reqId == sm->lastId ||
668 eap_success_workaround(sm, sm->reqId, sm->lastId)))
669 SM_ENTER(EAP, SUCCESS);
670 else if (sm->methodState != METHOD_CONT &&
671 ((sm->rxFailure &&
672 sm->decision != DECISION_UNCOND_SUCC) ||
673 (sm->rxSuccess && sm->decision == DECISION_FAIL &&
674 (sm->selectedMethod != EAP_TYPE_LEAP ||
675 sm->methodState != METHOD_MAY_CONT))) &&
676 (sm->reqId == sm->lastId ||
677 eap_success_workaround(sm, sm->reqId, sm->lastId)))
678 SM_ENTER(EAP, FAILURE);
679 else if (sm->rxReq && duplicate)
680 SM_ENTER(EAP, RETRANSMIT);
681 else if (sm->rxReq && !duplicate &&
682 sm->reqMethod == EAP_TYPE_NOTIFICATION &&
683 sm->allowNotifications)
684 SM_ENTER(EAP, NOTIFICATION);
685 else if (sm->rxReq && !duplicate &&
686 sm->selectedMethod == EAP_TYPE_NONE &&
687 sm->reqMethod == EAP_TYPE_IDENTITY)
688 SM_ENTER(EAP, IDENTITY);
689 else if (sm->rxReq && !duplicate &&
690 sm->selectedMethod == EAP_TYPE_NONE &&
691 sm->reqMethod != EAP_TYPE_IDENTITY &&
692 sm->reqMethod != EAP_TYPE_NOTIFICATION)
693 SM_ENTER(EAP, GET_METHOD);
694 else if (sm->rxReq && !duplicate &&
695 sm->reqMethod == sm->selectedMethod &&
696 sm->methodState != METHOD_DONE)
697 SM_ENTER(EAP, METHOD);
698 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
699 (sm->rxSuccess || sm->rxResp))
700 SM_ENTER(EAP, METHOD);
701 else
702 SM_ENTER(EAP, DISCARD);
703}
704
705
706static void eap_peer_sm_step_local(struct eap_sm *sm)
707{
708 switch (sm->EAP_state) {
709 case EAP_INITIALIZE:
710 SM_ENTER(EAP, IDLE);
711 break;
712 case EAP_DISABLED:
713 if (eapol_get_bool(sm, EAPOL_portEnabled) &&
714 !sm->force_disabled)
715 SM_ENTER(EAP, INITIALIZE);
716 break;
717 case EAP_IDLE:
718 eap_peer_sm_step_idle(sm);
719 break;
720 case EAP_RECEIVED:
721 eap_peer_sm_step_received(sm);
722 break;
723 case EAP_GET_METHOD:
724 if (sm->selectedMethod == sm->reqMethod)
725 SM_ENTER(EAP, METHOD);
726 else
727 SM_ENTER(EAP, SEND_RESPONSE);
728 break;
729 case EAP_METHOD:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800730 /*
731 * Note: RFC 4137 uses methodState == DONE && decision == FAIL
732 * as the condition. eapRespData == NULL here is used to allow
733 * final EAP method response to be sent without having to change
734 * all methods to either use methodState MAY_CONT or leaving
735 * decision to something else than FAIL in cases where the only
736 * expected response is EAP-Failure.
737 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 if (sm->ignore)
739 SM_ENTER(EAP, DISCARD);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800740 else if (sm->methodState == METHOD_DONE &&
741 sm->decision == DECISION_FAIL && !sm->eapRespData)
742 SM_ENTER(EAP, FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743 else
744 SM_ENTER(EAP, SEND_RESPONSE);
745 break;
746 case EAP_SEND_RESPONSE:
747 SM_ENTER(EAP, IDLE);
748 break;
749 case EAP_DISCARD:
750 SM_ENTER(EAP, IDLE);
751 break;
752 case EAP_IDENTITY:
753 SM_ENTER(EAP, SEND_RESPONSE);
754 break;
755 case EAP_NOTIFICATION:
756 SM_ENTER(EAP, SEND_RESPONSE);
757 break;
758 case EAP_RETRANSMIT:
759 SM_ENTER(EAP, SEND_RESPONSE);
760 break;
761 case EAP_SUCCESS:
762 break;
763 case EAP_FAILURE:
764 break;
765 }
766}
767
768
769SM_STEP(EAP)
770{
771 /* Global transitions */
772 if (eapol_get_bool(sm, EAPOL_eapRestart) &&
773 eapol_get_bool(sm, EAPOL_portEnabled))
774 SM_ENTER_GLOBAL(EAP, INITIALIZE);
775 else if (!eapol_get_bool(sm, EAPOL_portEnabled) || sm->force_disabled)
776 SM_ENTER_GLOBAL(EAP, DISABLED);
777 else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
778 /* RFC 4137 does not place any limit on number of EAP messages
779 * in an authentication session. However, some error cases have
780 * ended up in a state were EAP messages were sent between the
781 * peer and server in a loop (e.g., TLS ACK frame in both
782 * direction). Since this is quite undesired outcome, limit the
783 * total number of EAP round-trips and abort authentication if
784 * this limit is exceeded.
785 */
786 if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
787 wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: more than %d "
788 "authentication rounds - abort",
789 EAP_MAX_AUTH_ROUNDS);
790 sm->num_rounds++;
791 SM_ENTER_GLOBAL(EAP, FAILURE);
792 }
793 } else {
794 /* Local transitions */
795 eap_peer_sm_step_local(sm);
796 }
797}
798
799
800static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
801 EapType method)
802{
803 if (!eap_allowed_method(sm, vendor, method)) {
804 wpa_printf(MSG_DEBUG, "EAP: configuration does not allow: "
805 "vendor %u method %u", vendor, method);
806 return FALSE;
807 }
808 if (eap_peer_get_eap_method(vendor, method))
809 return TRUE;
810 wpa_printf(MSG_DEBUG, "EAP: not included in build: "
811 "vendor %u method %u", vendor, method);
812 return FALSE;
813}
814
815
816static struct wpabuf * eap_sm_build_expanded_nak(
817 struct eap_sm *sm, int id, const struct eap_method *methods,
818 size_t count)
819{
820 struct wpabuf *resp;
821 int found = 0;
822 const struct eap_method *m;
823
824 wpa_printf(MSG_DEBUG, "EAP: Building expanded EAP-Nak");
825
826 /* RFC 3748 - 5.3.2: Expanded Nak */
827 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_EXPANDED,
828 8 + 8 * (count + 1), EAP_CODE_RESPONSE, id);
829 if (resp == NULL)
830 return NULL;
831
832 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
833 wpabuf_put_be32(resp, EAP_TYPE_NAK);
834
835 for (m = methods; m; m = m->next) {
836 if (sm->reqVendor == m->vendor &&
837 sm->reqVendorMethod == m->method)
838 continue; /* do not allow the current method again */
839 if (eap_allowed_method(sm, m->vendor, m->method)) {
840 wpa_printf(MSG_DEBUG, "EAP: allowed type: "
841 "vendor=%u method=%u",
842 m->vendor, m->method);
843 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
844 wpabuf_put_be24(resp, m->vendor);
845 wpabuf_put_be32(resp, m->method);
846
847 found++;
848 }
849 }
850 if (!found) {
851 wpa_printf(MSG_DEBUG, "EAP: no more allowed methods");
852 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
853 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
854 wpabuf_put_be32(resp, EAP_TYPE_NONE);
855 }
856
857 eap_update_len(resp);
858
859 return resp;
860}
861
862
863static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id)
864{
865 struct wpabuf *resp;
866 u8 *start;
867 int found = 0, expanded_found = 0;
868 size_t count;
869 const struct eap_method *methods, *m;
870
871 wpa_printf(MSG_DEBUG, "EAP: Building EAP-Nak (requested type %u "
872 "vendor=%u method=%u not allowed)", sm->reqMethod,
873 sm->reqVendor, sm->reqVendorMethod);
874 methods = eap_peer_get_methods(&count);
875 if (methods == NULL)
876 return NULL;
877 if (sm->reqMethod == EAP_TYPE_EXPANDED)
878 return eap_sm_build_expanded_nak(sm, id, methods, count);
879
880 /* RFC 3748 - 5.3.1: Legacy Nak */
881 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK,
882 sizeof(struct eap_hdr) + 1 + count + 1,
883 EAP_CODE_RESPONSE, id);
884 if (resp == NULL)
885 return NULL;
886
887 start = wpabuf_put(resp, 0);
888 for (m = methods; m; m = m->next) {
889 if (m->vendor == EAP_VENDOR_IETF && m->method == sm->reqMethod)
890 continue; /* do not allow the current method again */
891 if (eap_allowed_method(sm, m->vendor, m->method)) {
892 if (m->vendor != EAP_VENDOR_IETF) {
893 if (expanded_found)
894 continue;
895 expanded_found = 1;
896 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
897 } else
898 wpabuf_put_u8(resp, m->method);
899 found++;
900 }
901 }
902 if (!found)
903 wpabuf_put_u8(resp, EAP_TYPE_NONE);
904 wpa_hexdump(MSG_DEBUG, "EAP: allowed methods", start, found);
905
906 eap_update_len(resp);
907
908 return resp;
909}
910
911
912static void eap_sm_processIdentity(struct eap_sm *sm, const struct wpabuf *req)
913{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700914 const u8 *pos;
915 size_t msg_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916
917 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
918 "EAP authentication started");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800919 eap_notify_status(sm, "started", "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700921 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, req,
922 &msg_len);
923 if (pos == NULL)
924 return;
925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 /*
927 * RFC 3748 - 5.1: Identity
928 * Data field may contain a displayable message in UTF-8. If this
929 * includes NUL-character, only the data before that should be
930 * displayed. Some EAP implementasitons may piggy-back additional
931 * options after the NUL.
932 */
933 /* TODO: could save displayable message so that it can be shown to the
934 * user in case of interaction is required */
935 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Identity data",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700936 pos, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937}
938
939
940#ifdef PCSC_FUNCS
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800941
942/*
943 * Rules for figuring out MNC length based on IMSI for SIM cards that do not
944 * include MNC length field.
945 */
946static int mnc_len_from_imsi(const char *imsi)
947{
948 char mcc_str[4];
949 unsigned int mcc;
950
951 os_memcpy(mcc_str, imsi, 3);
952 mcc_str[3] = '\0';
953 mcc = atoi(mcc_str);
954
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700955 if (mcc == 228)
956 return 2; /* Networks in Switzerland use 2-digit MNC */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800957 if (mcc == 244)
958 return 2; /* Networks in Finland use 2-digit MNC */
959
960 return -1;
961}
962
963
964static int eap_sm_append_3gpp_realm(struct eap_sm *sm, char *imsi,
965 size_t max_len, size_t *imsi_len)
966{
967 int mnc_len;
968 char *pos, mnc[4];
969
970 if (*imsi_len + 36 > max_len) {
971 wpa_printf(MSG_WARNING, "No room for realm in IMSI buffer");
972 return -1;
973 }
974
975 /* MNC (2 or 3 digits) */
976 mnc_len = scard_get_mnc_len(sm->scard_ctx);
977 if (mnc_len < 0)
978 mnc_len = mnc_len_from_imsi(imsi);
979 if (mnc_len < 0) {
980 wpa_printf(MSG_INFO, "Failed to get MNC length from (U)SIM "
981 "assuming 3");
982 mnc_len = 3;
983 }
984
985 if (mnc_len == 2) {
986 mnc[0] = '0';
987 mnc[1] = imsi[3];
988 mnc[2] = imsi[4];
989 } else if (mnc_len == 3) {
990 mnc[0] = imsi[3];
991 mnc[1] = imsi[4];
992 mnc[2] = imsi[5];
993 }
994 mnc[3] = '\0';
995
996 pos = imsi + *imsi_len;
997 pos += os_snprintf(pos, imsi + max_len - pos,
998 "@wlan.mnc%s.mcc%c%c%c.3gppnetwork.org",
999 mnc, imsi[0], imsi[1], imsi[2]);
1000 *imsi_len = pos - imsi;
1001
1002 return 0;
1003}
1004
1005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006static int eap_sm_imsi_identity(struct eap_sm *sm,
1007 struct eap_peer_config *conf)
1008{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001009 enum { EAP_SM_SIM, EAP_SM_AKA, EAP_SM_AKA_PRIME } method = EAP_SM_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 char imsi[100];
1011 size_t imsi_len;
1012 struct eap_method_type *m = conf->eap_methods;
1013 int i;
1014
1015 imsi_len = sizeof(imsi);
1016 if (scard_get_imsi(sm->scard_ctx, imsi, &imsi_len)) {
1017 wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM");
1018 return -1;
1019 }
1020
1021 wpa_hexdump_ascii(MSG_DEBUG, "IMSI", (u8 *) imsi, imsi_len);
1022
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001023 if (imsi_len < 7) {
1024 wpa_printf(MSG_WARNING, "Too short IMSI for SIM identity");
1025 return -1;
1026 }
1027
1028 if (eap_sm_append_3gpp_realm(sm, imsi, sizeof(imsi), &imsi_len) < 0) {
1029 wpa_printf(MSG_WARNING, "Could not add realm to SIM identity");
1030 return -1;
1031 }
1032 wpa_hexdump_ascii(MSG_DEBUG, "IMSI + realm", (u8 *) imsi, imsi_len);
1033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 for (i = 0; m && (m[i].vendor != EAP_VENDOR_IETF ||
1035 m[i].method != EAP_TYPE_NONE); i++) {
1036 if (m[i].vendor == EAP_VENDOR_IETF &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07001037 m[i].method == EAP_TYPE_AKA_PRIME) {
1038 method = EAP_SM_AKA_PRIME;
1039 break;
1040 }
1041
1042 if (m[i].vendor == EAP_VENDOR_IETF &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 m[i].method == EAP_TYPE_AKA) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001044 method = EAP_SM_AKA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 break;
1046 }
1047 }
1048
1049 os_free(conf->identity);
1050 conf->identity = os_malloc(1 + imsi_len);
1051 if (conf->identity == NULL) {
1052 wpa_printf(MSG_WARNING, "Failed to allocate buffer for "
1053 "IMSI-based identity");
1054 return -1;
1055 }
1056
Dmitry Shmidt04949592012-07-19 12:16:46 -07001057 switch (method) {
1058 case EAP_SM_SIM:
1059 conf->identity[0] = '1';
1060 break;
1061 case EAP_SM_AKA:
1062 conf->identity[0] = '0';
1063 break;
1064 case EAP_SM_AKA_PRIME:
1065 conf->identity[0] = '6';
1066 break;
1067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 os_memcpy(conf->identity + 1, imsi, imsi_len);
1069 conf->identity_len = 1 + imsi_len;
1070
1071 return 0;
1072}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074#endif /* PCSC_FUNCS */
1075
1076
1077static int eap_sm_set_scard_pin(struct eap_sm *sm,
1078 struct eap_peer_config *conf)
1079{
1080#ifdef PCSC_FUNCS
1081 if (scard_set_pin(sm->scard_ctx, conf->pin)) {
1082 /*
1083 * Make sure the same PIN is not tried again in order to avoid
1084 * blocking SIM.
1085 */
1086 os_free(conf->pin);
1087 conf->pin = NULL;
1088
1089 wpa_printf(MSG_WARNING, "PIN validation failed");
1090 eap_sm_request_pin(sm);
1091 return -1;
1092 }
1093 return 0;
1094#else /* PCSC_FUNCS */
1095 return -1;
1096#endif /* PCSC_FUNCS */
1097}
1098
1099static int eap_sm_get_scard_identity(struct eap_sm *sm,
1100 struct eap_peer_config *conf)
1101{
1102#ifdef PCSC_FUNCS
1103 if (eap_sm_set_scard_pin(sm, conf))
1104 return -1;
1105
1106 return eap_sm_imsi_identity(sm, conf);
1107#else /* PCSC_FUNCS */
1108 return -1;
1109#endif /* PCSC_FUNCS */
1110}
1111
1112
1113/**
1114 * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
1115 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1116 * @id: EAP identifier for the packet
1117 * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
1118 * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
1119 * failure
1120 *
1121 * This function allocates and builds an EAP-Identity/Response packet for the
1122 * current network. The caller is responsible for freeing the returned data.
1123 */
1124struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted)
1125{
1126 struct eap_peer_config *config = eap_get_config(sm);
1127 struct wpabuf *resp;
1128 const u8 *identity;
1129 size_t identity_len;
1130
1131 if (config == NULL) {
1132 wpa_printf(MSG_WARNING, "EAP: buildIdentity: configuration "
1133 "was not available");
1134 return NULL;
1135 }
1136
1137 if (sm->m && sm->m->get_identity &&
1138 (identity = sm->m->get_identity(sm, sm->eap_method_priv,
1139 &identity_len)) != NULL) {
1140 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth "
1141 "identity", identity, identity_len);
1142 } else if (!encrypted && config->anonymous_identity) {
1143 identity = config->anonymous_identity;
1144 identity_len = config->anonymous_identity_len;
1145 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity",
1146 identity, identity_len);
1147 } else {
1148 identity = config->identity;
1149 identity_len = config->identity_len;
1150 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity",
1151 identity, identity_len);
1152 }
1153
1154 if (identity == NULL) {
1155 wpa_printf(MSG_WARNING, "EAP: buildIdentity: identity "
1156 "configuration was not available");
1157 if (config->pcsc) {
1158 if (eap_sm_get_scard_identity(sm, config) < 0)
1159 return NULL;
1160 identity = config->identity;
1161 identity_len = config->identity_len;
1162 wpa_hexdump_ascii(MSG_DEBUG, "permanent identity from "
1163 "IMSI", identity, identity_len);
1164 } else {
1165 eap_sm_request_identity(sm);
1166 return NULL;
1167 }
1168 } else if (config->pcsc) {
1169 if (eap_sm_set_scard_pin(sm, config) < 0)
1170 return NULL;
1171 }
1172
1173 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, identity_len,
1174 EAP_CODE_RESPONSE, id);
1175 if (resp == NULL)
1176 return NULL;
1177
1178 wpabuf_put_data(resp, identity, identity_len);
1179
1180 return resp;
1181}
1182
1183
1184static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req)
1185{
1186 const u8 *pos;
1187 char *msg;
1188 size_t i, msg_len;
1189
1190 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, req,
1191 &msg_len);
1192 if (pos == NULL)
1193 return;
1194 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Notification data",
1195 pos, msg_len);
1196
1197 msg = os_malloc(msg_len + 1);
1198 if (msg == NULL)
1199 return;
1200 for (i = 0; i < msg_len; i++)
1201 msg[i] = isprint(pos[i]) ? (char) pos[i] : '_';
1202 msg[msg_len] = '\0';
1203 wpa_msg(sm->msg_ctx, MSG_INFO, "%s%s",
1204 WPA_EVENT_EAP_NOTIFICATION, msg);
1205 os_free(msg);
1206}
1207
1208
1209static struct wpabuf * eap_sm_buildNotify(int id)
1210{
1211 struct wpabuf *resp;
1212
1213 wpa_printf(MSG_DEBUG, "EAP: Generating EAP-Response Notification");
1214 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, 0,
1215 EAP_CODE_RESPONSE, id);
1216 if (resp == NULL)
1217 return NULL;
1218
1219 return resp;
1220}
1221
1222
1223static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req)
1224{
1225 const struct eap_hdr *hdr;
1226 size_t plen;
1227 const u8 *pos;
1228
1229 sm->rxReq = sm->rxResp = sm->rxSuccess = sm->rxFailure = FALSE;
1230 sm->reqId = 0;
1231 sm->reqMethod = EAP_TYPE_NONE;
1232 sm->reqVendor = EAP_VENDOR_IETF;
1233 sm->reqVendorMethod = EAP_TYPE_NONE;
1234
1235 if (req == NULL || wpabuf_len(req) < sizeof(*hdr))
1236 return;
1237
1238 hdr = wpabuf_head(req);
1239 plen = be_to_host16(hdr->length);
1240 if (plen > wpabuf_len(req)) {
1241 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
1242 "(len=%lu plen=%lu)",
1243 (unsigned long) wpabuf_len(req),
1244 (unsigned long) plen);
1245 return;
1246 }
1247
1248 sm->reqId = hdr->identifier;
1249
1250 if (sm->workaround) {
1251 const u8 *addr[1];
1252 addr[0] = wpabuf_head(req);
1253 md5_vector(1, addr, &plen, sm->req_md5);
1254 }
1255
1256 switch (hdr->code) {
1257 case EAP_CODE_REQUEST:
1258 if (plen < sizeof(*hdr) + 1) {
1259 wpa_printf(MSG_DEBUG, "EAP: Too short EAP-Request - "
1260 "no Type field");
1261 return;
1262 }
1263 sm->rxReq = TRUE;
1264 pos = (const u8 *) (hdr + 1);
1265 sm->reqMethod = *pos++;
1266 if (sm->reqMethod == EAP_TYPE_EXPANDED) {
1267 if (plen < sizeof(*hdr) + 8) {
1268 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
1269 "expanded EAP-Packet (plen=%lu)",
1270 (unsigned long) plen);
1271 return;
1272 }
1273 sm->reqVendor = WPA_GET_BE24(pos);
1274 pos += 3;
1275 sm->reqVendorMethod = WPA_GET_BE32(pos);
1276 }
1277 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Request id=%d "
1278 "method=%u vendor=%u vendorMethod=%u",
1279 sm->reqId, sm->reqMethod, sm->reqVendor,
1280 sm->reqVendorMethod);
1281 break;
1282 case EAP_CODE_RESPONSE:
1283 if (sm->selectedMethod == EAP_TYPE_LEAP) {
1284 /*
1285 * LEAP differs from RFC 4137 by using reversed roles
1286 * for mutual authentication and because of this, we
1287 * need to accept EAP-Response frames if LEAP is used.
1288 */
1289 if (plen < sizeof(*hdr) + 1) {
1290 wpa_printf(MSG_DEBUG, "EAP: Too short "
1291 "EAP-Response - no Type field");
1292 return;
1293 }
1294 sm->rxResp = TRUE;
1295 pos = (const u8 *) (hdr + 1);
1296 sm->reqMethod = *pos;
1297 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Response for "
1298 "LEAP method=%d id=%d",
1299 sm->reqMethod, sm->reqId);
1300 break;
1301 }
1302 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Response");
1303 break;
1304 case EAP_CODE_SUCCESS:
1305 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Success");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001306 eap_notify_status(sm, "completion", "success");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 sm->rxSuccess = TRUE;
1308 break;
1309 case EAP_CODE_FAILURE:
1310 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Failure");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001311 eap_notify_status(sm, "completion", "failure");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 sm->rxFailure = TRUE;
1313 break;
1314 default:
1315 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Packet with unknown "
1316 "code %d", hdr->code);
1317 break;
1318 }
1319}
1320
1321
1322static void eap_peer_sm_tls_event(void *ctx, enum tls_event ev,
1323 union tls_event_data *data)
1324{
1325 struct eap_sm *sm = ctx;
1326 char *hash_hex = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001327
1328 switch (ev) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001329 case TLS_CERT_CHAIN_SUCCESS:
1330 eap_notify_status(sm, "remote certificate verification",
1331 "success");
1332 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333 case TLS_CERT_CHAIN_FAILURE:
1334 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_TLS_CERT_ERROR
1335 "reason=%d depth=%d subject='%s' err='%s'",
1336 data->cert_fail.reason,
1337 data->cert_fail.depth,
1338 data->cert_fail.subject,
1339 data->cert_fail.reason_txt);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001340 eap_notify_status(sm, "remote certificate verification",
1341 data->cert_fail.reason_txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 break;
1343 case TLS_PEER_CERTIFICATE:
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001344 if (!sm->eapol_cb->notify_cert)
1345 break;
1346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347 if (data->peer_cert.hash) {
1348 size_t len = data->peer_cert.hash_len * 2 + 1;
1349 hash_hex = os_malloc(len);
1350 if (hash_hex) {
1351 wpa_snprintf_hex(hash_hex, len,
1352 data->peer_cert.hash,
1353 data->peer_cert.hash_len);
1354 }
1355 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001357 sm->eapol_cb->notify_cert(sm->eapol_ctx,
1358 data->peer_cert.depth,
1359 data->peer_cert.subject,
1360 hash_hex, data->peer_cert.cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001362 case TLS_ALERT:
1363 if (data->alert.is_local)
1364 eap_notify_status(sm, "local TLS alert",
1365 data->alert.description);
1366 else
1367 eap_notify_status(sm, "remote TLS alert",
1368 data->alert.description);
1369 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 }
1371
1372 os_free(hash_hex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373}
1374
1375
1376/**
1377 * eap_peer_sm_init - Allocate and initialize EAP peer state machine
1378 * @eapol_ctx: Context data to be used with eapol_cb calls
1379 * @eapol_cb: Pointer to EAPOL callback functions
1380 * @msg_ctx: Context data for wpa_msg() calls
1381 * @conf: EAP configuration
1382 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
1383 *
1384 * This function allocates and initializes an EAP state machine. In addition,
1385 * this initializes TLS library for the new EAP state machine. eapol_cb pointer
1386 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
1387 * state machine. Consequently, the caller must make sure that this data
1388 * structure remains alive while the EAP state machine is active.
1389 */
1390struct eap_sm * eap_peer_sm_init(void *eapol_ctx,
1391 struct eapol_callbacks *eapol_cb,
1392 void *msg_ctx, struct eap_config *conf)
1393{
1394 struct eap_sm *sm;
1395 struct tls_config tlsconf;
1396
1397 sm = os_zalloc(sizeof(*sm));
1398 if (sm == NULL)
1399 return NULL;
1400 sm->eapol_ctx = eapol_ctx;
1401 sm->eapol_cb = eapol_cb;
1402 sm->msg_ctx = msg_ctx;
1403 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
1404 sm->wps = conf->wps;
1405
1406 os_memset(&tlsconf, 0, sizeof(tlsconf));
1407 tlsconf.opensc_engine_path = conf->opensc_engine_path;
1408 tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path;
1409 tlsconf.pkcs11_module_path = conf->pkcs11_module_path;
1410#ifdef CONFIG_FIPS
1411 tlsconf.fips_mode = 1;
1412#endif /* CONFIG_FIPS */
1413 tlsconf.event_cb = eap_peer_sm_tls_event;
1414 tlsconf.cb_ctx = sm;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001415 tlsconf.cert_in_cb = conf->cert_in_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 sm->ssl_ctx = tls_init(&tlsconf);
1417 if (sm->ssl_ctx == NULL) {
1418 wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS "
1419 "context.");
1420 os_free(sm);
1421 return NULL;
1422 }
1423
Dmitry Shmidt04949592012-07-19 12:16:46 -07001424 sm->ssl_ctx2 = tls_init(&tlsconf);
1425 if (sm->ssl_ctx2 == NULL) {
1426 wpa_printf(MSG_INFO, "SSL: Failed to initialize TLS "
1427 "context (2).");
1428 /* Run without separate TLS context within TLS tunnel */
1429 }
1430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 return sm;
1432}
1433
1434
1435/**
1436 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
1437 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1438 *
1439 * This function deinitializes EAP state machine and frees all allocated
1440 * resources.
1441 */
1442void eap_peer_sm_deinit(struct eap_sm *sm)
1443{
1444 if (sm == NULL)
1445 return;
1446 eap_deinit_prev_method(sm, "EAP deinit");
1447 eap_sm_abort(sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001448 if (sm->ssl_ctx2)
1449 tls_deinit(sm->ssl_ctx2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 tls_deinit(sm->ssl_ctx);
1451 os_free(sm);
1452}
1453
1454
1455/**
1456 * eap_peer_sm_step - Step EAP peer state machine
1457 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1458 * Returns: 1 if EAP state was changed or 0 if not
1459 *
1460 * This function advances EAP state machine to a new state to match with the
1461 * current variables. This should be called whenever variables used by the EAP
1462 * state machine have changed.
1463 */
1464int eap_peer_sm_step(struct eap_sm *sm)
1465{
1466 int res = 0;
1467 do {
1468 sm->changed = FALSE;
1469 SM_STEP_RUN(EAP);
1470 if (sm->changed)
1471 res = 1;
1472 } while (sm->changed);
1473 return res;
1474}
1475
1476
1477/**
1478 * eap_sm_abort - Abort EAP authentication
1479 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1480 *
1481 * Release system resources that have been allocated for the authentication
1482 * session without fully deinitializing the EAP state machine.
1483 */
1484void eap_sm_abort(struct eap_sm *sm)
1485{
1486 wpabuf_free(sm->lastRespData);
1487 sm->lastRespData = NULL;
1488 wpabuf_free(sm->eapRespData);
1489 sm->eapRespData = NULL;
1490 os_free(sm->eapKeyData);
1491 sm->eapKeyData = NULL;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001492 os_free(sm->eapSessionId);
1493 sm->eapSessionId = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494
1495 /* This is not clearly specified in the EAP statemachines draft, but
1496 * it seems necessary to make sure that some of the EAPOL variables get
1497 * cleared for the next authentication. */
1498 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
1499}
1500
1501
1502#ifdef CONFIG_CTRL_IFACE
1503static const char * eap_sm_state_txt(int state)
1504{
1505 switch (state) {
1506 case EAP_INITIALIZE:
1507 return "INITIALIZE";
1508 case EAP_DISABLED:
1509 return "DISABLED";
1510 case EAP_IDLE:
1511 return "IDLE";
1512 case EAP_RECEIVED:
1513 return "RECEIVED";
1514 case EAP_GET_METHOD:
1515 return "GET_METHOD";
1516 case EAP_METHOD:
1517 return "METHOD";
1518 case EAP_SEND_RESPONSE:
1519 return "SEND_RESPONSE";
1520 case EAP_DISCARD:
1521 return "DISCARD";
1522 case EAP_IDENTITY:
1523 return "IDENTITY";
1524 case EAP_NOTIFICATION:
1525 return "NOTIFICATION";
1526 case EAP_RETRANSMIT:
1527 return "RETRANSMIT";
1528 case EAP_SUCCESS:
1529 return "SUCCESS";
1530 case EAP_FAILURE:
1531 return "FAILURE";
1532 default:
1533 return "UNKNOWN";
1534 }
1535}
1536#endif /* CONFIG_CTRL_IFACE */
1537
1538
1539#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1540static const char * eap_sm_method_state_txt(EapMethodState state)
1541{
1542 switch (state) {
1543 case METHOD_NONE:
1544 return "NONE";
1545 case METHOD_INIT:
1546 return "INIT";
1547 case METHOD_CONT:
1548 return "CONT";
1549 case METHOD_MAY_CONT:
1550 return "MAY_CONT";
1551 case METHOD_DONE:
1552 return "DONE";
1553 default:
1554 return "UNKNOWN";
1555 }
1556}
1557
1558
1559static const char * eap_sm_decision_txt(EapDecision decision)
1560{
1561 switch (decision) {
1562 case DECISION_FAIL:
1563 return "FAIL";
1564 case DECISION_COND_SUCC:
1565 return "COND_SUCC";
1566 case DECISION_UNCOND_SUCC:
1567 return "UNCOND_SUCC";
1568 default:
1569 return "UNKNOWN";
1570 }
1571}
1572#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1573
1574
1575#ifdef CONFIG_CTRL_IFACE
1576
1577/**
1578 * eap_sm_get_status - Get EAP state machine status
1579 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1580 * @buf: Buffer for status information
1581 * @buflen: Maximum buffer length
1582 * @verbose: Whether to include verbose status information
1583 * Returns: Number of bytes written to buf.
1584 *
1585 * Query EAP state machine for status information. This function fills in a
1586 * text area with current status information from the EAPOL state machine. If
1587 * the buffer (buf) is not large enough, status information will be truncated
1588 * to fit the buffer.
1589 */
1590int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose)
1591{
1592 int len, ret;
1593
1594 if (sm == NULL)
1595 return 0;
1596
1597 len = os_snprintf(buf, buflen,
1598 "EAP state=%s\n",
1599 eap_sm_state_txt(sm->EAP_state));
1600 if (len < 0 || (size_t) len >= buflen)
1601 return 0;
1602
1603 if (sm->selectedMethod != EAP_TYPE_NONE) {
1604 const char *name;
1605 if (sm->m) {
1606 name = sm->m->name;
1607 } else {
1608 const struct eap_method *m =
1609 eap_peer_get_eap_method(EAP_VENDOR_IETF,
1610 sm->selectedMethod);
1611 if (m)
1612 name = m->name;
1613 else
1614 name = "?";
1615 }
1616 ret = os_snprintf(buf + len, buflen - len,
1617 "selectedMethod=%d (EAP-%s)\n",
1618 sm->selectedMethod, name);
1619 if (ret < 0 || (size_t) ret >= buflen - len)
1620 return len;
1621 len += ret;
1622
1623 if (sm->m && sm->m->get_status) {
1624 len += sm->m->get_status(sm, sm->eap_method_priv,
1625 buf + len, buflen - len,
1626 verbose);
1627 }
1628 }
1629
1630 if (verbose) {
1631 ret = os_snprintf(buf + len, buflen - len,
1632 "reqMethod=%d\n"
1633 "methodState=%s\n"
1634 "decision=%s\n"
1635 "ClientTimeout=%d\n",
1636 sm->reqMethod,
1637 eap_sm_method_state_txt(sm->methodState),
1638 eap_sm_decision_txt(sm->decision),
1639 sm->ClientTimeout);
1640 if (ret < 0 || (size_t) ret >= buflen - len)
1641 return len;
1642 len += ret;
1643 }
1644
1645 return len;
1646}
1647#endif /* CONFIG_CTRL_IFACE */
1648
1649
1650#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001651static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652 const char *msg, size_t msglen)
1653{
1654 struct eap_peer_config *config;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001655 const char *txt = NULL;
1656 char *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657
1658 if (sm == NULL)
1659 return;
1660 config = eap_get_config(sm);
1661 if (config == NULL)
1662 return;
1663
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001664 switch (field) {
1665 case WPA_CTRL_REQ_EAP_IDENTITY:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 config->pending_req_identity++;
1667 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001668 case WPA_CTRL_REQ_EAP_PASSWORD:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 config->pending_req_password++;
1670 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001671 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672 config->pending_req_new_password++;
1673 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001674 case WPA_CTRL_REQ_EAP_PIN:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 config->pending_req_pin++;
1676 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001677 case WPA_CTRL_REQ_EAP_OTP:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 if (msg) {
1679 tmp = os_malloc(msglen + 3);
1680 if (tmp == NULL)
1681 return;
1682 tmp[0] = '[';
1683 os_memcpy(tmp + 1, msg, msglen);
1684 tmp[msglen + 1] = ']';
1685 tmp[msglen + 2] = '\0';
1686 txt = tmp;
1687 os_free(config->pending_req_otp);
1688 config->pending_req_otp = tmp;
1689 config->pending_req_otp_len = msglen + 3;
1690 } else {
1691 if (config->pending_req_otp == NULL)
1692 return;
1693 txt = config->pending_req_otp;
1694 }
1695 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001696 case WPA_CTRL_REQ_EAP_PASSPHRASE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697 config->pending_req_passphrase++;
1698 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001699 case WPA_CTRL_REQ_SIM:
1700 txt = msg;
1701 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 default:
1703 return;
1704 }
1705
1706 if (sm->eapol_cb->eap_param_needed)
1707 sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt);
1708}
1709#else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1710#define eap_sm_request(sm, type, msg, msglen) do { } while (0)
1711#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1712
1713const char * eap_sm_get_method_name(struct eap_sm *sm)
1714{
1715 if (sm->m == NULL)
1716 return "UNKNOWN";
1717 return sm->m->name;
1718}
1719
1720
1721/**
1722 * eap_sm_request_identity - Request identity from user (ctrl_iface)
1723 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1724 *
1725 * EAP methods can call this function to request identity information for the
1726 * current network. This is normally called when the identity is not included
1727 * in the network configuration. The request will be sent to monitor programs
1728 * through the control interface.
1729 */
1730void eap_sm_request_identity(struct eap_sm *sm)
1731{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001732 eap_sm_request(sm, WPA_CTRL_REQ_EAP_IDENTITY, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733}
1734
1735
1736/**
1737 * eap_sm_request_password - Request password from user (ctrl_iface)
1738 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1739 *
1740 * EAP methods can call this function to request password information for the
1741 * current network. This is normally called when the password is not included
1742 * in the network configuration. The request will be sent to monitor programs
1743 * through the control interface.
1744 */
1745void eap_sm_request_password(struct eap_sm *sm)
1746{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001747 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSWORD, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748}
1749
1750
1751/**
1752 * eap_sm_request_new_password - Request new password from user (ctrl_iface)
1753 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1754 *
1755 * EAP methods can call this function to request new password information for
1756 * the current network. This is normally called when the EAP method indicates
1757 * that the current password has expired and password change is required. The
1758 * request will be sent to monitor programs through the control interface.
1759 */
1760void eap_sm_request_new_password(struct eap_sm *sm)
1761{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001762 eap_sm_request(sm, WPA_CTRL_REQ_EAP_NEW_PASSWORD, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763}
1764
1765
1766/**
1767 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
1768 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1769 *
1770 * EAP methods can call this function to request SIM or smart card PIN
1771 * information for the current network. This is normally called when the PIN is
1772 * not included in the network configuration. The request will be sent to
1773 * monitor programs through the control interface.
1774 */
1775void eap_sm_request_pin(struct eap_sm *sm)
1776{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001777 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PIN, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001778}
1779
1780
1781/**
1782 * eap_sm_request_otp - Request one time password from user (ctrl_iface)
1783 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1784 * @msg: Message to be displayed to the user when asking for OTP
1785 * @msg_len: Length of the user displayable message
1786 *
1787 * EAP methods can call this function to request open time password (OTP) for
1788 * the current network. The request will be sent to monitor programs through
1789 * the control interface.
1790 */
1791void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len)
1792{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001793 eap_sm_request(sm, WPA_CTRL_REQ_EAP_OTP, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794}
1795
1796
1797/**
1798 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
1799 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1800 *
1801 * EAP methods can call this function to request passphrase for a private key
1802 * for the current network. This is normally called when the passphrase is not
1803 * included in the network configuration. The request will be sent to monitor
1804 * programs through the control interface.
1805 */
1806void eap_sm_request_passphrase(struct eap_sm *sm)
1807{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001808 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSPHRASE, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809}
1810
1811
1812/**
Dmitry Shmidt051af732013-10-22 13:52:46 -07001813 * eap_sm_request_sim - Request external SIM processing
1814 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1815 * @req: EAP method specific request
1816 */
1817void eap_sm_request_sim(struct eap_sm *sm, const char *req)
1818{
1819 eap_sm_request(sm, WPA_CTRL_REQ_SIM, req, os_strlen(req));
1820}
1821
1822
1823/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824 * eap_sm_notify_ctrl_attached - Notification of attached monitor
1825 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1826 *
1827 * Notify EAP state machines that a monitor was attached to the control
1828 * interface to trigger re-sending of pending requests for user input.
1829 */
1830void eap_sm_notify_ctrl_attached(struct eap_sm *sm)
1831{
1832 struct eap_peer_config *config = eap_get_config(sm);
1833
1834 if (config == NULL)
1835 return;
1836
1837 /* Re-send any pending requests for user data since a new control
1838 * interface was added. This handles cases where the EAP authentication
1839 * starts immediately after system startup when the user interface is
1840 * not yet running. */
1841 if (config->pending_req_identity)
1842 eap_sm_request_identity(sm);
1843 if (config->pending_req_password)
1844 eap_sm_request_password(sm);
1845 if (config->pending_req_new_password)
1846 eap_sm_request_new_password(sm);
1847 if (config->pending_req_otp)
1848 eap_sm_request_otp(sm, NULL, 0);
1849 if (config->pending_req_pin)
1850 eap_sm_request_pin(sm);
1851 if (config->pending_req_passphrase)
1852 eap_sm_request_passphrase(sm);
1853}
1854
1855
1856static int eap_allowed_phase2_type(int vendor, int type)
1857{
1858 if (vendor != EAP_VENDOR_IETF)
1859 return 0;
1860 return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS &&
1861 type != EAP_TYPE_FAST;
1862}
1863
1864
1865/**
1866 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
1867 * @name: EAP method name, e.g., MD5
1868 * @vendor: Buffer for returning EAP Vendor-Id
1869 * Returns: EAP method type or %EAP_TYPE_NONE if not found
1870 *
1871 * This function maps EAP type names into EAP type numbers that are allowed for
1872 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
1873 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
1874 */
1875u32 eap_get_phase2_type(const char *name, int *vendor)
1876{
1877 int v;
1878 u8 type = eap_peer_get_type(name, &v);
1879 if (eap_allowed_phase2_type(v, type)) {
1880 *vendor = v;
1881 return type;
1882 }
1883 *vendor = EAP_VENDOR_IETF;
1884 return EAP_TYPE_NONE;
1885}
1886
1887
1888/**
1889 * eap_get_phase2_types - Get list of allowed EAP phase 2 types
1890 * @config: Pointer to a network configuration
1891 * @count: Pointer to a variable to be filled with number of returned EAP types
1892 * Returns: Pointer to allocated type list or %NULL on failure
1893 *
1894 * This function generates an array of allowed EAP phase 2 (tunneled) types for
1895 * the given network configuration.
1896 */
1897struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config,
1898 size_t *count)
1899{
1900 struct eap_method_type *buf;
1901 u32 method;
1902 int vendor;
1903 size_t mcount;
1904 const struct eap_method *methods, *m;
1905
1906 methods = eap_peer_get_methods(&mcount);
1907 if (methods == NULL)
1908 return NULL;
1909 *count = 0;
1910 buf = os_malloc(mcount * sizeof(struct eap_method_type));
1911 if (buf == NULL)
1912 return NULL;
1913
1914 for (m = methods; m; m = m->next) {
1915 vendor = m->vendor;
1916 method = m->method;
1917 if (eap_allowed_phase2_type(vendor, method)) {
1918 if (vendor == EAP_VENDOR_IETF &&
1919 method == EAP_TYPE_TLS && config &&
1920 config->private_key2 == NULL)
1921 continue;
1922 buf[*count].vendor = vendor;
1923 buf[*count].method = method;
1924 (*count)++;
1925 }
1926 }
1927
1928 return buf;
1929}
1930
1931
1932/**
1933 * eap_set_fast_reauth - Update fast_reauth setting
1934 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1935 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
1936 */
1937void eap_set_fast_reauth(struct eap_sm *sm, int enabled)
1938{
1939 sm->fast_reauth = enabled;
1940}
1941
1942
1943/**
1944 * eap_set_workaround - Update EAP workarounds setting
1945 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1946 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
1947 */
1948void eap_set_workaround(struct eap_sm *sm, unsigned int workaround)
1949{
1950 sm->workaround = workaround;
1951}
1952
1953
1954/**
1955 * eap_get_config - Get current network configuration
1956 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1957 * Returns: Pointer to the current network configuration or %NULL if not found
1958 *
1959 * EAP peer methods should avoid using this function if they can use other
1960 * access functions, like eap_get_config_identity() and
1961 * eap_get_config_password(), that do not require direct access to
1962 * struct eap_peer_config.
1963 */
1964struct eap_peer_config * eap_get_config(struct eap_sm *sm)
1965{
1966 return sm->eapol_cb->get_config(sm->eapol_ctx);
1967}
1968
1969
1970/**
1971 * eap_get_config_identity - Get identity from the network configuration
1972 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1973 * @len: Buffer for the length of the identity
1974 * Returns: Pointer to the identity or %NULL if not found
1975 */
1976const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
1977{
1978 struct eap_peer_config *config = eap_get_config(sm);
1979 if (config == NULL)
1980 return NULL;
1981 *len = config->identity_len;
1982 return config->identity;
1983}
1984
1985
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001986static int eap_get_ext_password(struct eap_sm *sm,
1987 struct eap_peer_config *config)
1988{
1989 char *name;
1990
1991 if (config->password == NULL)
1992 return -1;
1993
1994 name = os_zalloc(config->password_len + 1);
1995 if (name == NULL)
1996 return -1;
1997 os_memcpy(name, config->password, config->password_len);
1998
1999 ext_password_free(sm->ext_pw_buf);
2000 sm->ext_pw_buf = ext_password_get(sm->ext_pw, name);
2001 os_free(name);
2002
2003 return sm->ext_pw_buf == NULL ? -1 : 0;
2004}
2005
2006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002007/**
2008 * eap_get_config_password - Get password from the network configuration
2009 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2010 * @len: Buffer for the length of the password
2011 * Returns: Pointer to the password or %NULL if not found
2012 */
2013const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
2014{
2015 struct eap_peer_config *config = eap_get_config(sm);
2016 if (config == NULL)
2017 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002018
2019 if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
2020 if (eap_get_ext_password(sm, config) < 0)
2021 return NULL;
2022 *len = wpabuf_len(sm->ext_pw_buf);
2023 return wpabuf_head(sm->ext_pw_buf);
2024 }
2025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026 *len = config->password_len;
2027 return config->password;
2028}
2029
2030
2031/**
2032 * eap_get_config_password2 - Get password from the network configuration
2033 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2034 * @len: Buffer for the length of the password
2035 * @hash: Buffer for returning whether the password is stored as a
2036 * NtPasswordHash instead of plaintext password; can be %NULL if this
2037 * information is not needed
2038 * Returns: Pointer to the password or %NULL if not found
2039 */
2040const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
2041{
2042 struct eap_peer_config *config = eap_get_config(sm);
2043 if (config == NULL)
2044 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002045
2046 if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
2047 if (eap_get_ext_password(sm, config) < 0)
2048 return NULL;
2049 *len = wpabuf_len(sm->ext_pw_buf);
2050 return wpabuf_head(sm->ext_pw_buf);
2051 }
2052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 *len = config->password_len;
2054 if (hash)
2055 *hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH);
2056 return config->password;
2057}
2058
2059
2060/**
2061 * eap_get_config_new_password - Get new password from network configuration
2062 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2063 * @len: Buffer for the length of the new password
2064 * Returns: Pointer to the new password or %NULL if not found
2065 */
2066const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
2067{
2068 struct eap_peer_config *config = eap_get_config(sm);
2069 if (config == NULL)
2070 return NULL;
2071 *len = config->new_password_len;
2072 return config->new_password;
2073}
2074
2075
2076/**
2077 * eap_get_config_otp - Get one-time password from the network configuration
2078 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2079 * @len: Buffer for the length of the one-time password
2080 * Returns: Pointer to the one-time password or %NULL if not found
2081 */
2082const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len)
2083{
2084 struct eap_peer_config *config = eap_get_config(sm);
2085 if (config == NULL)
2086 return NULL;
2087 *len = config->otp_len;
2088 return config->otp;
2089}
2090
2091
2092/**
2093 * eap_clear_config_otp - Clear used one-time password
2094 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2095 *
2096 * This function clears a used one-time password (OTP) from the current network
2097 * configuration. This should be called when the OTP has been used and is not
2098 * needed anymore.
2099 */
2100void eap_clear_config_otp(struct eap_sm *sm)
2101{
2102 struct eap_peer_config *config = eap_get_config(sm);
2103 if (config == NULL)
2104 return;
2105 os_memset(config->otp, 0, config->otp_len);
2106 os_free(config->otp);
2107 config->otp = NULL;
2108 config->otp_len = 0;
2109}
2110
2111
2112/**
2113 * eap_get_config_phase1 - Get phase1 data from the network configuration
2114 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2115 * Returns: Pointer to the phase1 data or %NULL if not found
2116 */
2117const char * eap_get_config_phase1(struct eap_sm *sm)
2118{
2119 struct eap_peer_config *config = eap_get_config(sm);
2120 if (config == NULL)
2121 return NULL;
2122 return config->phase1;
2123}
2124
2125
2126/**
2127 * eap_get_config_phase2 - Get phase2 data from the network configuration
2128 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2129 * Returns: Pointer to the phase1 data or %NULL if not found
2130 */
2131const char * eap_get_config_phase2(struct eap_sm *sm)
2132{
2133 struct eap_peer_config *config = eap_get_config(sm);
2134 if (config == NULL)
2135 return NULL;
2136 return config->phase2;
2137}
2138
2139
2140int eap_get_config_fragment_size(struct eap_sm *sm)
2141{
2142 struct eap_peer_config *config = eap_get_config(sm);
2143 if (config == NULL)
2144 return -1;
2145 return config->fragment_size;
2146}
2147
2148
2149/**
2150 * eap_key_available - Get key availability (eapKeyAvailable variable)
2151 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2152 * Returns: 1 if EAP keying material is available, 0 if not
2153 */
2154int eap_key_available(struct eap_sm *sm)
2155{
2156 return sm ? sm->eapKeyAvailable : 0;
2157}
2158
2159
2160/**
2161 * eap_notify_success - Notify EAP state machine about external success trigger
2162 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2163 *
2164 * This function is called when external event, e.g., successful completion of
2165 * WPA-PSK key handshake, is indicating that EAP state machine should move to
2166 * success state. This is mainly used with security modes that do not use EAP
2167 * state machine (e.g., WPA-PSK).
2168 */
2169void eap_notify_success(struct eap_sm *sm)
2170{
2171 if (sm) {
2172 sm->decision = DECISION_COND_SUCC;
2173 sm->EAP_state = EAP_SUCCESS;
2174 }
2175}
2176
2177
2178/**
2179 * eap_notify_lower_layer_success - Notification of lower layer success
2180 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2181 *
2182 * Notify EAP state machines that a lower layer has detected a successful
2183 * authentication. This is used to recover from dropped EAP-Success messages.
2184 */
2185void eap_notify_lower_layer_success(struct eap_sm *sm)
2186{
2187 if (sm == NULL)
2188 return;
2189
2190 if (eapol_get_bool(sm, EAPOL_eapSuccess) ||
2191 sm->decision == DECISION_FAIL ||
2192 (sm->methodState != METHOD_MAY_CONT &&
2193 sm->methodState != METHOD_DONE))
2194 return;
2195
2196 if (sm->eapKeyData != NULL)
2197 sm->eapKeyAvailable = TRUE;
2198 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
2199 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
2200 "EAP authentication completed successfully (based on lower "
2201 "layer success)");
2202}
2203
2204
2205/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002206 * eap_get_eapSessionId - Get Session-Id from EAP state machine
2207 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2208 * @len: Pointer to variable that will be set to number of bytes in the session
2209 * Returns: Pointer to the EAP Session-Id or %NULL on failure
2210 *
2211 * Fetch EAP Session-Id from the EAP state machine. The Session-Id is available
2212 * only after a successful authentication. EAP state machine continues to manage
2213 * the Session-Id and the caller must not change or free the returned data.
2214 */
2215const u8 * eap_get_eapSessionId(struct eap_sm *sm, size_t *len)
2216{
2217 if (sm == NULL || sm->eapSessionId == NULL) {
2218 *len = 0;
2219 return NULL;
2220 }
2221
2222 *len = sm->eapSessionIdLen;
2223 return sm->eapSessionId;
2224}
2225
2226
2227/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
2229 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2230 * @len: Pointer to variable that will be set to number of bytes in the key
2231 * Returns: Pointer to the EAP keying data or %NULL on failure
2232 *
2233 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
2234 * key is available only after a successful authentication. EAP state machine
2235 * continues to manage the key data and the caller must not change or free the
2236 * returned data.
2237 */
2238const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len)
2239{
2240 if (sm == NULL || sm->eapKeyData == NULL) {
2241 *len = 0;
2242 return NULL;
2243 }
2244
2245 *len = sm->eapKeyDataLen;
2246 return sm->eapKeyData;
2247}
2248
2249
2250/**
2251 * eap_get_eapKeyData - Get EAP response data
2252 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2253 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
2254 *
2255 * Fetch EAP response (eapRespData) from the EAP state machine. This data is
2256 * available when EAP state machine has processed an incoming EAP request. The
2257 * EAP state machine does not maintain a reference to the response after this
2258 * function is called and the caller is responsible for freeing the data.
2259 */
2260struct wpabuf * eap_get_eapRespData(struct eap_sm *sm)
2261{
2262 struct wpabuf *resp;
2263
2264 if (sm == NULL || sm->eapRespData == NULL)
2265 return NULL;
2266
2267 resp = sm->eapRespData;
2268 sm->eapRespData = NULL;
2269
2270 return resp;
2271}
2272
2273
2274/**
2275 * eap_sm_register_scard_ctx - Notification of smart card context
2276 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2277 * @ctx: Context data for smart card operations
2278 *
2279 * Notify EAP state machines of context data for smart card operations. This
2280 * context data will be used as a parameter for scard_*() functions.
2281 */
2282void eap_register_scard_ctx(struct eap_sm *sm, void *ctx)
2283{
2284 if (sm)
2285 sm->scard_ctx = ctx;
2286}
2287
2288
2289/**
2290 * eap_set_config_blob - Set or add a named configuration blob
2291 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2292 * @blob: New value for the blob
2293 *
2294 * Adds a new configuration blob or replaces the current value of an existing
2295 * blob.
2296 */
2297void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob)
2298{
2299#ifndef CONFIG_NO_CONFIG_BLOBS
2300 sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob);
2301#endif /* CONFIG_NO_CONFIG_BLOBS */
2302}
2303
2304
2305/**
2306 * eap_get_config_blob - Get a named configuration blob
2307 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2308 * @name: Name of the blob
2309 * Returns: Pointer to blob data or %NULL if not found
2310 */
2311const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm,
2312 const char *name)
2313{
2314#ifndef CONFIG_NO_CONFIG_BLOBS
2315 return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name);
2316#else /* CONFIG_NO_CONFIG_BLOBS */
2317 return NULL;
2318#endif /* CONFIG_NO_CONFIG_BLOBS */
2319}
2320
2321
2322/**
2323 * eap_set_force_disabled - Set force_disabled flag
2324 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2325 * @disabled: 1 = EAP disabled, 0 = EAP enabled
2326 *
2327 * This function is used to force EAP state machine to be disabled when it is
2328 * not in use (e.g., with WPA-PSK or plaintext connections).
2329 */
2330void eap_set_force_disabled(struct eap_sm *sm, int disabled)
2331{
2332 sm->force_disabled = disabled;
2333}
2334
2335
Dmitry Shmidt051af732013-10-22 13:52:46 -07002336/**
2337 * eap_set_external_sim - Set external_sim flag
2338 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2339 * @external_sim: Whether external SIM/USIM processing is used
2340 */
2341void eap_set_external_sim(struct eap_sm *sm, int external_sim)
2342{
2343 sm->external_sim = external_sim;
2344}
2345
2346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347 /**
2348 * eap_notify_pending - Notify that EAP method is ready to re-process a request
2349 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2350 *
2351 * An EAP method can perform a pending operation (e.g., to get a response from
2352 * an external process). Once the response is available, this function can be
2353 * used to request EAPOL state machine to retry delivering the previously
2354 * received (and still unanswered) EAP request to EAP state machine.
2355 */
2356void eap_notify_pending(struct eap_sm *sm)
2357{
2358 sm->eapol_cb->notify_pending(sm->eapol_ctx);
2359}
2360
2361
2362/**
2363 * eap_invalidate_cached_session - Mark cached session data invalid
2364 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2365 */
2366void eap_invalidate_cached_session(struct eap_sm *sm)
2367{
2368 if (sm)
2369 eap_deinit_prev_method(sm, "invalidate");
2370}
2371
2372
2373int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf)
2374{
2375 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2376 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2377 return 0; /* Not a WPS Enrollee */
2378
2379 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL)
2380 return 0; /* Not using PBC */
2381
2382 return 1;
2383}
2384
2385
2386int eap_is_wps_pin_enrollee(struct eap_peer_config *conf)
2387{
2388 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2389 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2390 return 0; /* Not a WPS Enrollee */
2391
2392 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL)
2393 return 0; /* Not using PIN */
2394
2395 return 1;
2396}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002397
2398
2399void eap_sm_set_ext_pw_ctx(struct eap_sm *sm, struct ext_password_data *ext)
2400{
2401 ext_password_free(sm->ext_pw_buf);
2402 sm->ext_pw_buf = NULL;
2403 sm->ext_pw = ext;
2404}
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002405
2406
2407/**
2408 * eap_set_anon_id - Set or add anonymous identity
2409 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2410 * @id: Anonymous identity (e.g., EAP-SIM pseudonym) or %NULL to clear
2411 * @len: Length of anonymous identity in octets
2412 */
2413void eap_set_anon_id(struct eap_sm *sm, const u8 *id, size_t len)
2414{
2415 if (sm->eapol_cb->set_anon_id)
2416 sm->eapol_cb->set_anon_id(sm->eapol_ctx, id, len);
2417}