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