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