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