blob: 28d5116fd511844a6c95949c41725ba181152337 [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"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080026#include "crypto/sha256.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "common/wpa_ctrl.h"
28#include "eap_common/eap_wsc_common.h"
29#include "eap_i.h"
30#include "eap_config.h"
31
32#define STATE_MACHINE_DATA struct eap_sm
33#define STATE_MACHINE_DEBUG_PREFIX "EAP"
34
35#define EAP_MAX_AUTH_ROUNDS 50
36#define EAP_CLIENT_TIMEOUT_DEFAULT 60
37
38
39static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
40 EapType method);
41static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id);
42static void eap_sm_processIdentity(struct eap_sm *sm,
43 const struct wpabuf *req);
44static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req);
45static struct wpabuf * eap_sm_buildNotify(int id);
46static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req);
47#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
48static const char * eap_sm_method_state_txt(EapMethodState state);
49static const char * eap_sm_decision_txt(EapDecision decision);
50#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
51
52
53
54static Boolean eapol_get_bool(struct eap_sm *sm, enum eapol_bool_var var)
55{
56 return sm->eapol_cb->get_bool(sm->eapol_ctx, var);
57}
58
59
60static void eapol_set_bool(struct eap_sm *sm, enum eapol_bool_var var,
61 Boolean value)
62{
63 sm->eapol_cb->set_bool(sm->eapol_ctx, var, value);
64}
65
66
67static unsigned int eapol_get_int(struct eap_sm *sm, enum eapol_int_var var)
68{
69 return sm->eapol_cb->get_int(sm->eapol_ctx, var);
70}
71
72
73static void eapol_set_int(struct eap_sm *sm, enum eapol_int_var var,
74 unsigned int value)
75{
76 sm->eapol_cb->set_int(sm->eapol_ctx, var, value);
77}
78
79
80static struct wpabuf * eapol_get_eapReqData(struct eap_sm *sm)
81{
82 return sm->eapol_cb->get_eapReqData(sm->eapol_ctx);
83}
84
85
Dmitry Shmidt04949592012-07-19 12:16:46 -070086static void eap_notify_status(struct eap_sm *sm, const char *status,
87 const char *parameter)
88{
89 wpa_printf(MSG_DEBUG, "EAP: Status notification: %s (param=%s)",
90 status, parameter);
91 if (sm->eapol_cb->notify_status)
92 sm->eapol_cb->notify_status(sm->eapol_ctx, status, parameter);
93}
94
95
Dmitry Shmidtc2817022014-07-02 10:32:10 -070096static void eap_sm_free_key(struct eap_sm *sm)
97{
98 if (sm->eapKeyData) {
99 bin_clear_free(sm->eapKeyData, sm->eapKeyDataLen);
100 sm->eapKeyData = NULL;
101 }
102}
103
104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105static void eap_deinit_prev_method(struct eap_sm *sm, const char *txt)
106{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107 ext_password_free(sm->ext_pw_buf);
108 sm->ext_pw_buf = NULL;
109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110 if (sm->m == NULL || sm->eap_method_priv == NULL)
111 return;
112
113 wpa_printf(MSG_DEBUG, "EAP: deinitialize previously used EAP method "
114 "(%d, %s) at %s", sm->selectedMethod, sm->m->name, txt);
115 sm->m->deinit(sm, sm->eap_method_priv);
116 sm->eap_method_priv = NULL;
117 sm->m = NULL;
118}
119
120
121/**
122 * eap_allowed_method - Check whether EAP method is allowed
123 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
124 * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
125 * @method: EAP type
126 * Returns: 1 = allowed EAP method, 0 = not allowed
127 */
128int eap_allowed_method(struct eap_sm *sm, int vendor, u32 method)
129{
130 struct eap_peer_config *config = eap_get_config(sm);
131 int i;
132 struct eap_method_type *m;
133
134 if (config == NULL || config->eap_methods == NULL)
135 return 1;
136
137 m = config->eap_methods;
138 for (i = 0; m[i].vendor != EAP_VENDOR_IETF ||
139 m[i].method != EAP_TYPE_NONE; i++) {
140 if (m[i].vendor == vendor && m[i].method == method)
141 return 1;
142 }
143 return 0;
144}
145
146
147/*
148 * This state initializes state machine variables when the machine is
149 * activated (portEnabled = TRUE). This is also used when re-starting
150 * authentication (eapRestart == TRUE).
151 */
152SM_STATE(EAP, INITIALIZE)
153{
154 SM_ENTRY(EAP, INITIALIZE);
155 if (sm->fast_reauth && sm->m && sm->m->has_reauth_data &&
156 sm->m->has_reauth_data(sm, sm->eap_method_priv) &&
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700157 !sm->prev_failure &&
158 sm->last_config == eap_get_config(sm)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 wpa_printf(MSG_DEBUG, "EAP: maintaining EAP method data for "
160 "fast reauthentication");
161 sm->m->deinit_for_reauth(sm, sm->eap_method_priv);
162 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700163 sm->last_config = eap_get_config(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164 eap_deinit_prev_method(sm, "INITIALIZE");
165 }
166 sm->selectedMethod = EAP_TYPE_NONE;
167 sm->methodState = METHOD_NONE;
168 sm->allowNotifications = TRUE;
169 sm->decision = DECISION_FAIL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800170 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
172 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
173 eapol_set_bool(sm, EAPOL_eapFail, FALSE);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700174 eap_sm_free_key(sm);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800175 os_free(sm->eapSessionId);
176 sm->eapSessionId = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 sm->eapKeyAvailable = FALSE;
178 eapol_set_bool(sm, EAPOL_eapRestart, FALSE);
179 sm->lastId = -1; /* new session - make sure this does not match with
180 * the first EAP-Packet */
181 /*
182 * RFC 4137 does not reset eapResp and eapNoResp here. However, this
183 * seemed to be able to trigger cases where both were set and if EAPOL
184 * state machine uses eapNoResp first, it may end up not sending a real
185 * reply correctly. This occurred when the workaround in FAIL state set
186 * eapNoResp = TRUE.. Maybe that workaround needs to be fixed to do
187 * something else(?)
188 */
189 eapol_set_bool(sm, EAPOL_eapResp, FALSE);
190 eapol_set_bool(sm, EAPOL_eapNoResp, FALSE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800191 /*
192 * RFC 4137 does not reset ignore here, but since it is possible for
193 * some method code paths to end up not setting ignore=FALSE, clear the
194 * value here to avoid issues if a previous authentication attempt
195 * failed with ignore=TRUE being left behind in the last
196 * m.check(eapReqData) operation.
197 */
198 sm->ignore = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 sm->num_rounds = 0;
200 sm->prev_failure = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800201 sm->expected_failure = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800202 sm->reauthInit = FALSE;
203 sm->erp_seq = (u32) -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204}
205
206
207/*
208 * This state is reached whenever service from the lower layer is interrupted
209 * or unavailable (portEnabled == FALSE). Immediate transition to INITIALIZE
210 * occurs when the port becomes enabled.
211 */
212SM_STATE(EAP, DISABLED)
213{
214 SM_ENTRY(EAP, DISABLED);
215 sm->num_rounds = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700216 /*
217 * RFC 4137 does not describe clearing of idleWhile here, but doing so
218 * allows the timer tick to be stopped more quickly when EAP is not in
219 * use.
220 */
221 eapol_set_int(sm, EAPOL_idleWhile, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222}
223
224
225/*
226 * The state machine spends most of its time here, waiting for something to
227 * happen. This state is entered unconditionally from INITIALIZE, DISCARD, and
228 * SEND_RESPONSE states.
229 */
230SM_STATE(EAP, IDLE)
231{
232 SM_ENTRY(EAP, IDLE);
233}
234
235
236/*
237 * This state is entered when an EAP packet is received (eapReq == TRUE) to
238 * parse the packet header.
239 */
240SM_STATE(EAP, RECEIVED)
241{
242 const struct wpabuf *eapReqData;
243
244 SM_ENTRY(EAP, RECEIVED);
245 eapReqData = eapol_get_eapReqData(sm);
246 /* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */
247 eap_sm_parseEapReq(sm, eapReqData);
248 sm->num_rounds++;
249}
250
251
252/*
253 * This state is entered when a request for a new type comes in. Either the
254 * correct method is started, or a Nak response is built.
255 */
256SM_STATE(EAP, GET_METHOD)
257{
258 int reinit;
259 EapType method;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700260 const struct eap_method *eap_method;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261
262 SM_ENTRY(EAP, GET_METHOD);
263
264 if (sm->reqMethod == EAP_TYPE_EXPANDED)
265 method = sm->reqVendorMethod;
266 else
267 method = sm->reqMethod;
268
Dmitry Shmidt04949592012-07-19 12:16:46 -0700269 eap_method = eap_peer_get_eap_method(sm->reqVendor, method);
270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 if (!eap_sm_allowMethod(sm, sm->reqVendor, method)) {
272 wpa_printf(MSG_DEBUG, "EAP: vendor %u method %u not allowed",
273 sm->reqVendor, method);
274 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
275 "vendor=%u method=%u -> NAK",
276 sm->reqVendor, method);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700277 eap_notify_status(sm, "refuse proposed method",
278 eap_method ? eap_method->name : "unknown");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 goto nak;
280 }
281
282 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
283 "vendor=%u method=%u", sm->reqVendor, method);
284
Dmitry Shmidt04949592012-07-19 12:16:46 -0700285 eap_notify_status(sm, "accept proposed method",
286 eap_method ? eap_method->name : "unknown");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 /*
288 * RFC 4137 does not define specific operation for fast
289 * re-authentication (session resumption). The design here is to allow
290 * the previously used method data to be maintained for
291 * re-authentication if the method support session resumption.
292 * Otherwise, the previously used method data is freed and a new method
293 * is allocated here.
294 */
295 if (sm->fast_reauth &&
296 sm->m && sm->m->vendor == sm->reqVendor &&
297 sm->m->method == method &&
298 sm->m->has_reauth_data &&
299 sm->m->has_reauth_data(sm, sm->eap_method_priv)) {
300 wpa_printf(MSG_DEBUG, "EAP: Using previous method data"
301 " for fast re-authentication");
302 reinit = 1;
303 } else {
304 eap_deinit_prev_method(sm, "GET_METHOD");
305 reinit = 0;
306 }
307
308 sm->selectedMethod = sm->reqMethod;
309 if (sm->m == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700310 sm->m = eap_method;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 if (!sm->m) {
312 wpa_printf(MSG_DEBUG, "EAP: Could not find selected method: "
313 "vendor %d method %d",
314 sm->reqVendor, method);
315 goto nak;
316 }
317
318 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
319
320 wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP method: "
321 "vendor %u method %u (%s)",
322 sm->reqVendor, method, sm->m->name);
323 if (reinit)
324 sm->eap_method_priv = sm->m->init_for_reauth(
325 sm, sm->eap_method_priv);
326 else
327 sm->eap_method_priv = sm->m->init(sm);
328
329 if (sm->eap_method_priv == NULL) {
330 struct eap_peer_config *config = eap_get_config(sm);
331 wpa_msg(sm->msg_ctx, MSG_INFO,
332 "EAP: Failed to initialize EAP method: vendor %u "
333 "method %u (%s)",
334 sm->reqVendor, method, sm->m->name);
335 sm->m = NULL;
336 sm->methodState = METHOD_NONE;
337 sm->selectedMethod = EAP_TYPE_NONE;
338 if (sm->reqMethod == EAP_TYPE_TLS && config &&
339 (config->pending_req_pin ||
340 config->pending_req_passphrase)) {
341 /*
342 * Return without generating Nak in order to allow
343 * entering of PIN code or passphrase to retry the
344 * current EAP packet.
345 */
346 wpa_printf(MSG_DEBUG, "EAP: Pending PIN/passphrase "
347 "request - skip Nak");
348 return;
349 }
350
351 goto nak;
352 }
353
354 sm->methodState = METHOD_INIT;
355 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_METHOD
356 "EAP vendor %u method %u (%s) selected",
357 sm->reqVendor, method, sm->m->name);
358 return;
359
360nak:
361 wpabuf_free(sm->eapRespData);
362 sm->eapRespData = NULL;
363 sm->eapRespData = eap_sm_buildNak(sm, sm->reqId);
364}
365
366
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800367#ifdef CONFIG_ERP
368
369static char * eap_home_realm(struct eap_sm *sm)
370{
371 struct eap_peer_config *config = eap_get_config(sm);
372 char *realm;
373 size_t i, realm_len;
374
375 if (!config)
376 return NULL;
377
378 if (config->identity) {
379 for (i = 0; i < config->identity_len; i++) {
380 if (config->identity[i] == '@')
381 break;
382 }
383 if (i < config->identity_len) {
384 realm_len = config->identity_len - i - 1;
385 realm = os_malloc(realm_len + 1);
386 if (realm == NULL)
387 return NULL;
388 os_memcpy(realm, &config->identity[i + 1], realm_len);
389 realm[realm_len] = '\0';
390 return realm;
391 }
392 }
393
394 if (config->anonymous_identity) {
395 for (i = 0; i < config->anonymous_identity_len; i++) {
396 if (config->anonymous_identity[i] == '@')
397 break;
398 }
399 if (i < config->anonymous_identity_len) {
400 realm_len = config->anonymous_identity_len - i - 1;
401 realm = os_malloc(realm_len + 1);
402 if (realm == NULL)
403 return NULL;
404 os_memcpy(realm, &config->anonymous_identity[i + 1],
405 realm_len);
406 realm[realm_len] = '\0';
407 return realm;
408 }
409 }
410
411 return os_strdup("");
412}
413
414
415static struct eap_erp_key *
416eap_erp_get_key(struct eap_sm *sm, const char *realm)
417{
418 struct eap_erp_key *erp;
419
420 dl_list_for_each(erp, &sm->erp_keys, struct eap_erp_key, list) {
421 char *pos;
422
423 pos = os_strchr(erp->keyname_nai, '@');
424 if (!pos)
425 continue;
426 pos++;
427 if (os_strcmp(pos, realm) == 0)
428 return erp;
429 }
430
431 return NULL;
432}
433
434
435static struct eap_erp_key *
436eap_erp_get_key_nai(struct eap_sm *sm, const char *nai)
437{
438 struct eap_erp_key *erp;
439
440 dl_list_for_each(erp, &sm->erp_keys, struct eap_erp_key, list) {
441 if (os_strcmp(erp->keyname_nai, nai) == 0)
442 return erp;
443 }
444
445 return NULL;
446}
447
448
449static void eap_peer_erp_free_key(struct eap_erp_key *erp)
450{
451 dl_list_del(&erp->list);
452 bin_clear_free(erp, sizeof(*erp));
453}
454
455
456static void eap_erp_remove_keys_realm(struct eap_sm *sm, const char *realm)
457{
458 struct eap_erp_key *erp;
459
460 while ((erp = eap_erp_get_key(sm, realm)) != NULL) {
461 wpa_printf(MSG_DEBUG, "EAP: Delete old ERP key %s",
462 erp->keyname_nai);
463 eap_peer_erp_free_key(erp);
464 }
465}
466
467#endif /* CONFIG_ERP */
468
469
470void eap_peer_erp_free_keys(struct eap_sm *sm)
471{
472#ifdef CONFIG_ERP
473 struct eap_erp_key *erp, *tmp;
474
475 dl_list_for_each_safe(erp, tmp, &sm->erp_keys, struct eap_erp_key, list)
476 eap_peer_erp_free_key(erp);
477#endif /* CONFIG_ERP */
478}
479
480
481static void eap_peer_erp_init(struct eap_sm *sm)
482{
483#ifdef CONFIG_ERP
484 u8 *emsk = NULL;
485 size_t emsk_len = 0;
486 u8 EMSKname[EAP_EMSK_NAME_LEN];
487 u8 len[2];
488 char *realm;
489 size_t realm_len, nai_buf_len;
490 struct eap_erp_key *erp = NULL;
491 int pos;
492
493 realm = eap_home_realm(sm);
494 if (!realm)
495 return;
496 realm_len = os_strlen(realm);
497 wpa_printf(MSG_DEBUG, "EAP: Realm for ERP keyName-NAI: %s", realm);
498 eap_erp_remove_keys_realm(sm, realm);
499
500 nai_buf_len = 2 * EAP_EMSK_NAME_LEN + 1 + realm_len;
501 if (nai_buf_len > 253) {
502 /*
503 * keyName-NAI has a maximum length of 253 octet to fit in
504 * RADIUS attributes.
505 */
506 wpa_printf(MSG_DEBUG,
507 "EAP: Too long realm for ERP keyName-NAI maximum length");
508 goto fail;
509 }
510 nai_buf_len++; /* null termination */
511 erp = os_zalloc(sizeof(*erp) + nai_buf_len);
512 if (erp == NULL)
513 goto fail;
514
515 emsk = sm->m->get_emsk(sm, sm->eap_method_priv, &emsk_len);
516 if (!emsk || emsk_len == 0 || emsk_len > ERP_MAX_KEY_LEN) {
517 wpa_printf(MSG_DEBUG,
518 "EAP: No suitable EMSK available for ERP");
519 goto fail;
520 }
521
522 wpa_hexdump_key(MSG_DEBUG, "EAP: EMSK", emsk, emsk_len);
523
524 WPA_PUT_BE16(len, 8);
525 if (hmac_sha256_kdf(sm->eapSessionId, sm->eapSessionIdLen, "EMSK",
526 len, sizeof(len),
527 EMSKname, EAP_EMSK_NAME_LEN) < 0) {
528 wpa_printf(MSG_DEBUG, "EAP: Could not derive EMSKname");
529 goto fail;
530 }
531 wpa_hexdump(MSG_DEBUG, "EAP: EMSKname", EMSKname, EAP_EMSK_NAME_LEN);
532
533 pos = wpa_snprintf_hex(erp->keyname_nai, nai_buf_len,
534 EMSKname, EAP_EMSK_NAME_LEN);
535 erp->keyname_nai[pos] = '@';
536 os_memcpy(&erp->keyname_nai[pos + 1], realm, realm_len);
537
538 WPA_PUT_BE16(len, emsk_len);
539 if (hmac_sha256_kdf(emsk, emsk_len,
540 "EAP Re-authentication Root Key@ietf.org",
541 len, sizeof(len), erp->rRK, emsk_len) < 0) {
542 wpa_printf(MSG_DEBUG, "EAP: Could not derive rRK for ERP");
543 goto fail;
544 }
545 erp->rRK_len = emsk_len;
546 wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rRK", erp->rRK, erp->rRK_len);
547
548 if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
549 "EAP Re-authentication Integrity Key@ietf.org",
550 len, sizeof(len), erp->rIK, erp->rRK_len) < 0) {
551 wpa_printf(MSG_DEBUG, "EAP: Could not derive rIK for ERP");
552 goto fail;
553 }
554 erp->rIK_len = erp->rRK_len;
555 wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rIK", erp->rIK, erp->rIK_len);
556
557 wpa_printf(MSG_DEBUG, "EAP: Stored ERP keys %s", erp->keyname_nai);
558 dl_list_add(&sm->erp_keys, &erp->list);
559 erp = NULL;
560fail:
561 bin_clear_free(emsk, emsk_len);
562 bin_clear_free(erp, sizeof(*erp));
563 os_free(realm);
564#endif /* CONFIG_ERP */
565}
566
567
568#ifdef CONFIG_ERP
569static int eap_peer_erp_reauth_start(struct eap_sm *sm,
570 const struct eap_hdr *hdr, size_t len)
571{
572 char *realm;
573 struct eap_erp_key *erp;
574 struct wpabuf *msg;
575 u8 hash[SHA256_MAC_LEN];
576
577 realm = eap_home_realm(sm);
578 if (!realm)
579 return -1;
580
581 erp = eap_erp_get_key(sm, realm);
582 os_free(realm);
583 realm = NULL;
584 if (!erp)
585 return -1;
586
587 if (erp->next_seq >= 65536)
588 return -1; /* SEQ has range of 0..65535 */
589
590 /* TODO: check rRK lifetime expiration */
591
592 wpa_printf(MSG_DEBUG, "EAP: Valid ERP key found %s (SEQ=%u)",
593 erp->keyname_nai, erp->next_seq);
594
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800595 msg = eap_msg_alloc(EAP_VENDOR_IETF, (EapType) EAP_ERP_TYPE_REAUTH,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800596 1 + 2 + 2 + os_strlen(erp->keyname_nai) + 1 + 16,
597 EAP_CODE_INITIATE, hdr->identifier);
598 if (msg == NULL)
599 return -1;
600
601 wpabuf_put_u8(msg, 0x20); /* Flags: R=0 B=0 L=1 */
602 wpabuf_put_be16(msg, erp->next_seq);
603
604 wpabuf_put_u8(msg, EAP_ERP_TLV_KEYNAME_NAI);
605 wpabuf_put_u8(msg, os_strlen(erp->keyname_nai));
606 wpabuf_put_str(msg, erp->keyname_nai);
607
608 wpabuf_put_u8(msg, EAP_ERP_CS_HMAC_SHA256_128); /* Cryptosuite */
609
610 if (hmac_sha256(erp->rIK, erp->rIK_len,
611 wpabuf_head(msg), wpabuf_len(msg), hash) < 0) {
612 wpabuf_free(msg);
613 return -1;
614 }
615 wpabuf_put_data(msg, hash, 16);
616
617 wpa_printf(MSG_DEBUG, "EAP: Sending EAP-Initiate/Re-auth");
618 sm->erp_seq = erp->next_seq;
619 erp->next_seq++;
620 wpabuf_free(sm->eapRespData);
621 sm->eapRespData = msg;
622 sm->reauthInit = TRUE;
623 return 0;
624}
625#endif /* CONFIG_ERP */
626
627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628/*
629 * The method processing happens here. The request from the authenticator is
630 * processed, and an appropriate response packet is built.
631 */
632SM_STATE(EAP, METHOD)
633{
634 struct wpabuf *eapReqData;
635 struct eap_method_ret ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800636 int min_len = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637
638 SM_ENTRY(EAP, METHOD);
639 if (sm->m == NULL) {
640 wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected");
641 return;
642 }
643
644 eapReqData = eapol_get_eapReqData(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800645 if (sm->m->vendor == EAP_VENDOR_IETF && sm->m->method == EAP_TYPE_LEAP)
646 min_len = 0; /* LEAP uses EAP-Success without payload */
647 if (!eap_hdr_len_valid(eapReqData, min_len))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700648 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649
650 /*
651 * Get ignore, methodState, decision, allowNotifications, and
652 * eapRespData. RFC 4137 uses three separate method procedure (check,
653 * process, and buildResp) in this state. These have been combined into
654 * a single function call to m->process() in order to optimize EAP
655 * method implementation interface a bit. These procedures are only
656 * used from within this METHOD state, so there is no need to keep
657 * these as separate C functions.
658 *
659 * The RFC 4137 procedures return values as follows:
660 * ignore = m.check(eapReqData)
661 * (methodState, decision, allowNotifications) = m.process(eapReqData)
662 * eapRespData = m.buildResp(reqId)
663 */
664 os_memset(&ret, 0, sizeof(ret));
665 ret.ignore = sm->ignore;
666 ret.methodState = sm->methodState;
667 ret.decision = sm->decision;
668 ret.allowNotifications = sm->allowNotifications;
669 wpabuf_free(sm->eapRespData);
670 sm->eapRespData = NULL;
671 sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret,
672 eapReqData);
673 wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s "
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800674 "methodState=%s decision=%s eapRespData=%p",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 ret.ignore ? "TRUE" : "FALSE",
676 eap_sm_method_state_txt(ret.methodState),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800677 eap_sm_decision_txt(ret.decision),
678 sm->eapRespData);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679
680 sm->ignore = ret.ignore;
681 if (sm->ignore)
682 return;
683 sm->methodState = ret.methodState;
684 sm->decision = ret.decision;
685 sm->allowNotifications = ret.allowNotifications;
686
687 if (sm->m->isKeyAvailable && sm->m->getKey &&
688 sm->m->isKeyAvailable(sm, sm->eap_method_priv)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800689 struct eap_peer_config *config = eap_get_config(sm);
690
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700691 eap_sm_free_key(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700692 sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv,
693 &sm->eapKeyDataLen);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800694 os_free(sm->eapSessionId);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700695 sm->eapSessionId = NULL;
696 if (sm->m->getSessionId) {
697 sm->eapSessionId = sm->m->getSessionId(
698 sm, sm->eap_method_priv,
699 &sm->eapSessionIdLen);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800700 wpa_hexdump(MSG_DEBUG, "EAP: Session-Id",
701 sm->eapSessionId, sm->eapSessionIdLen);
702 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800703 if (config->erp && sm->m->get_emsk && sm->eapSessionId)
704 eap_peer_erp_init(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700705 }
706}
707
708
709/*
710 * This state signals the lower layer that a response packet is ready to be
711 * sent.
712 */
713SM_STATE(EAP, SEND_RESPONSE)
714{
715 SM_ENTRY(EAP, SEND_RESPONSE);
716 wpabuf_free(sm->lastRespData);
717 if (sm->eapRespData) {
718 if (sm->workaround)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800719 os_memcpy(sm->last_sha1, sm->req_sha1, 20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720 sm->lastId = sm->reqId;
721 sm->lastRespData = wpabuf_dup(sm->eapRespData);
722 eapol_set_bool(sm, EAPOL_eapResp, TRUE);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800723 } else {
724 wpa_printf(MSG_DEBUG, "EAP: No eapRespData available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725 sm->lastRespData = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800726 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
728 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800729 sm->reauthInit = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730}
731
732
733/*
734 * This state signals the lower layer that the request was discarded, and no
735 * response packet will be sent at this time.
736 */
737SM_STATE(EAP, DISCARD)
738{
739 SM_ENTRY(EAP, DISCARD);
740 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
741 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
742}
743
744
745/*
746 * Handles requests for Identity method and builds a response.
747 */
748SM_STATE(EAP, IDENTITY)
749{
750 const struct wpabuf *eapReqData;
751
752 SM_ENTRY(EAP, IDENTITY);
753 eapReqData = eapol_get_eapReqData(sm);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700754 if (!eap_hdr_len_valid(eapReqData, 1))
755 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756 eap_sm_processIdentity(sm, eapReqData);
757 wpabuf_free(sm->eapRespData);
758 sm->eapRespData = NULL;
759 sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId, 0);
760}
761
762
763/*
764 * Handles requests for Notification method and builds a response.
765 */
766SM_STATE(EAP, NOTIFICATION)
767{
768 const struct wpabuf *eapReqData;
769
770 SM_ENTRY(EAP, NOTIFICATION);
771 eapReqData = eapol_get_eapReqData(sm);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700772 if (!eap_hdr_len_valid(eapReqData, 1))
773 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 eap_sm_processNotify(sm, eapReqData);
775 wpabuf_free(sm->eapRespData);
776 sm->eapRespData = NULL;
777 sm->eapRespData = eap_sm_buildNotify(sm->reqId);
778}
779
780
781/*
782 * This state retransmits the previous response packet.
783 */
784SM_STATE(EAP, RETRANSMIT)
785{
786 SM_ENTRY(EAP, RETRANSMIT);
787 wpabuf_free(sm->eapRespData);
788 if (sm->lastRespData)
789 sm->eapRespData = wpabuf_dup(sm->lastRespData);
790 else
791 sm->eapRespData = NULL;
792}
793
794
795/*
796 * This state is entered in case of a successful completion of authentication
797 * and state machine waits here until port is disabled or EAP authentication is
798 * restarted.
799 */
800SM_STATE(EAP, SUCCESS)
801{
802 SM_ENTRY(EAP, SUCCESS);
803 if (sm->eapKeyData != NULL)
804 sm->eapKeyAvailable = TRUE;
805 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
806
807 /*
808 * RFC 4137 does not clear eapReq here, but this seems to be required
809 * to avoid processing the same request twice when state machine is
810 * initialized.
811 */
812 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
813
814 /*
815 * RFC 4137 does not set eapNoResp here, but this seems to be required
816 * to get EAPOL Supplicant backend state machine into SUCCESS state. In
817 * addition, either eapResp or eapNoResp is required to be set after
818 * processing the received EAP frame.
819 */
820 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
821
822 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
823 "EAP authentication completed successfully");
824}
825
826
827/*
828 * This state is entered in case of a failure and state machine waits here
829 * until port is disabled or EAP authentication is restarted.
830 */
831SM_STATE(EAP, FAILURE)
832{
833 SM_ENTRY(EAP, FAILURE);
834 eapol_set_bool(sm, EAPOL_eapFail, TRUE);
835
836 /*
837 * RFC 4137 does not clear eapReq here, but this seems to be required
838 * to avoid processing the same request twice when state machine is
839 * initialized.
840 */
841 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
842
843 /*
844 * RFC 4137 does not set eapNoResp here. However, either eapResp or
845 * eapNoResp is required to be set after processing the received EAP
846 * frame.
847 */
848 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
849
850 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
851 "EAP authentication failed");
852
853 sm->prev_failure = 1;
854}
855
856
857static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId)
858{
859 /*
860 * At least Microsoft IAS and Meetinghouse Aegis seem to be sending
861 * EAP-Success/Failure with lastId + 1 even though RFC 3748 and
862 * RFC 4137 require that reqId == lastId. In addition, it looks like
863 * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
864 *
865 * Accept this kind of Id if EAP workarounds are enabled. These are
866 * unauthenticated plaintext messages, so this should have minimal
867 * security implications (bit easier to fake EAP-Success/Failure).
868 */
869 if (sm->workaround && (reqId == ((lastId + 1) & 0xff) ||
870 reqId == ((lastId + 2) & 0xff))) {
871 wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected "
872 "identifier field in EAP Success: "
873 "reqId=%d lastId=%d (these are supposed to be "
874 "same)", reqId, lastId);
875 return 1;
876 }
877 wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d "
878 "lastId=%d", reqId, lastId);
879 return 0;
880}
881
882
883/*
884 * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
885 */
886
887static void eap_peer_sm_step_idle(struct eap_sm *sm)
888{
889 /*
890 * The first three transitions are from RFC 4137. The last two are
891 * local additions to handle special cases with LEAP and PEAP server
892 * not sending EAP-Success in some cases.
893 */
894 if (eapol_get_bool(sm, EAPOL_eapReq))
895 SM_ENTER(EAP, RECEIVED);
896 else if ((eapol_get_bool(sm, EAPOL_altAccept) &&
897 sm->decision != DECISION_FAIL) ||
898 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
899 sm->decision == DECISION_UNCOND_SUCC))
900 SM_ENTER(EAP, SUCCESS);
901 else if (eapol_get_bool(sm, EAPOL_altReject) ||
902 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
903 sm->decision != DECISION_UNCOND_SUCC) ||
904 (eapol_get_bool(sm, EAPOL_altAccept) &&
905 sm->methodState != METHOD_CONT &&
906 sm->decision == DECISION_FAIL))
907 SM_ENTER(EAP, FAILURE);
908 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
909 sm->leap_done && sm->decision != DECISION_FAIL &&
910 sm->methodState == METHOD_DONE)
911 SM_ENTER(EAP, SUCCESS);
912 else if (sm->selectedMethod == EAP_TYPE_PEAP &&
913 sm->peap_done && sm->decision != DECISION_FAIL &&
914 sm->methodState == METHOD_DONE)
915 SM_ENTER(EAP, SUCCESS);
916}
917
918
919static int eap_peer_req_is_duplicate(struct eap_sm *sm)
920{
921 int duplicate;
922
923 duplicate = (sm->reqId == sm->lastId) && sm->rxReq;
924 if (sm->workaround && duplicate &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800925 os_memcmp(sm->req_sha1, sm->last_sha1, 20) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 /*
927 * RFC 4137 uses (reqId == lastId) as the only verification for
928 * duplicate EAP requests. However, this misses cases where the
929 * AS is incorrectly using the same id again; and
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800930 * unfortunately, such implementations exist. Use SHA1 hash as
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 * an extra verification for the packets being duplicate to
932 * workaround these issues.
933 */
934 wpa_printf(MSG_DEBUG, "EAP: AS used the same Id again, but "
935 "EAP packets were not identical");
936 wpa_printf(MSG_DEBUG, "EAP: workaround - assume this is not a "
937 "duplicate packet");
938 duplicate = 0;
939 }
940
941 return duplicate;
942}
943
944
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800945static int eap_peer_sm_allow_canned(struct eap_sm *sm)
946{
947 struct eap_peer_config *config = eap_get_config(sm);
948
949 return config && config->phase1 &&
950 os_strstr(config->phase1, "allow_canned_success=1");
951}
952
953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954static void eap_peer_sm_step_received(struct eap_sm *sm)
955{
956 int duplicate = eap_peer_req_is_duplicate(sm);
957
958 /*
959 * Two special cases below for LEAP are local additions to work around
960 * odd LEAP behavior (EAP-Success in the middle of authentication and
961 * then swapped roles). Other transitions are based on RFC 4137.
962 */
963 if (sm->rxSuccess && sm->decision != DECISION_FAIL &&
964 (sm->reqId == sm->lastId ||
965 eap_success_workaround(sm, sm->reqId, sm->lastId)))
966 SM_ENTER(EAP, SUCCESS);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800967 else if (sm->workaround && sm->lastId == -1 && sm->rxSuccess &&
968 !sm->rxFailure && !sm->rxReq && eap_peer_sm_allow_canned(sm))
969 SM_ENTER(EAP, SUCCESS); /* EAP-Success prior any EAP method */
970 else if (sm->workaround && sm->lastId == -1 && sm->rxFailure &&
971 !sm->rxReq && sm->methodState != METHOD_CONT &&
972 eap_peer_sm_allow_canned(sm))
973 SM_ENTER(EAP, FAILURE); /* EAP-Failure prior any EAP method */
974 else if (sm->workaround && sm->rxSuccess && !sm->rxFailure &&
975 !sm->rxReq && sm->methodState != METHOD_CONT &&
976 eap_peer_sm_allow_canned(sm))
977 SM_ENTER(EAP, SUCCESS); /* EAP-Success after Identity */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 else if (sm->methodState != METHOD_CONT &&
979 ((sm->rxFailure &&
980 sm->decision != DECISION_UNCOND_SUCC) ||
981 (sm->rxSuccess && sm->decision == DECISION_FAIL &&
982 (sm->selectedMethod != EAP_TYPE_LEAP ||
983 sm->methodState != METHOD_MAY_CONT))) &&
984 (sm->reqId == sm->lastId ||
985 eap_success_workaround(sm, sm->reqId, sm->lastId)))
986 SM_ENTER(EAP, FAILURE);
987 else if (sm->rxReq && duplicate)
988 SM_ENTER(EAP, RETRANSMIT);
989 else if (sm->rxReq && !duplicate &&
990 sm->reqMethod == EAP_TYPE_NOTIFICATION &&
991 sm->allowNotifications)
992 SM_ENTER(EAP, NOTIFICATION);
993 else if (sm->rxReq && !duplicate &&
994 sm->selectedMethod == EAP_TYPE_NONE &&
995 sm->reqMethod == EAP_TYPE_IDENTITY)
996 SM_ENTER(EAP, IDENTITY);
997 else if (sm->rxReq && !duplicate &&
998 sm->selectedMethod == EAP_TYPE_NONE &&
999 sm->reqMethod != EAP_TYPE_IDENTITY &&
1000 sm->reqMethod != EAP_TYPE_NOTIFICATION)
1001 SM_ENTER(EAP, GET_METHOD);
1002 else if (sm->rxReq && !duplicate &&
1003 sm->reqMethod == sm->selectedMethod &&
1004 sm->methodState != METHOD_DONE)
1005 SM_ENTER(EAP, METHOD);
1006 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
1007 (sm->rxSuccess || sm->rxResp))
1008 SM_ENTER(EAP, METHOD);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001009 else if (sm->reauthInit)
1010 SM_ENTER(EAP, SEND_RESPONSE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 else
1012 SM_ENTER(EAP, DISCARD);
1013}
1014
1015
1016static void eap_peer_sm_step_local(struct eap_sm *sm)
1017{
1018 switch (sm->EAP_state) {
1019 case EAP_INITIALIZE:
1020 SM_ENTER(EAP, IDLE);
1021 break;
1022 case EAP_DISABLED:
1023 if (eapol_get_bool(sm, EAPOL_portEnabled) &&
1024 !sm->force_disabled)
1025 SM_ENTER(EAP, INITIALIZE);
1026 break;
1027 case EAP_IDLE:
1028 eap_peer_sm_step_idle(sm);
1029 break;
1030 case EAP_RECEIVED:
1031 eap_peer_sm_step_received(sm);
1032 break;
1033 case EAP_GET_METHOD:
1034 if (sm->selectedMethod == sm->reqMethod)
1035 SM_ENTER(EAP, METHOD);
1036 else
1037 SM_ENTER(EAP, SEND_RESPONSE);
1038 break;
1039 case EAP_METHOD:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001040 /*
1041 * Note: RFC 4137 uses methodState == DONE && decision == FAIL
1042 * as the condition. eapRespData == NULL here is used to allow
1043 * final EAP method response to be sent without having to change
1044 * all methods to either use methodState MAY_CONT or leaving
1045 * decision to something else than FAIL in cases where the only
1046 * expected response is EAP-Failure.
1047 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 if (sm->ignore)
1049 SM_ENTER(EAP, DISCARD);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001050 else if (sm->methodState == METHOD_DONE &&
1051 sm->decision == DECISION_FAIL && !sm->eapRespData)
1052 SM_ENTER(EAP, FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 else
1054 SM_ENTER(EAP, SEND_RESPONSE);
1055 break;
1056 case EAP_SEND_RESPONSE:
1057 SM_ENTER(EAP, IDLE);
1058 break;
1059 case EAP_DISCARD:
1060 SM_ENTER(EAP, IDLE);
1061 break;
1062 case EAP_IDENTITY:
1063 SM_ENTER(EAP, SEND_RESPONSE);
1064 break;
1065 case EAP_NOTIFICATION:
1066 SM_ENTER(EAP, SEND_RESPONSE);
1067 break;
1068 case EAP_RETRANSMIT:
1069 SM_ENTER(EAP, SEND_RESPONSE);
1070 break;
1071 case EAP_SUCCESS:
1072 break;
1073 case EAP_FAILURE:
1074 break;
1075 }
1076}
1077
1078
1079SM_STEP(EAP)
1080{
1081 /* Global transitions */
1082 if (eapol_get_bool(sm, EAPOL_eapRestart) &&
1083 eapol_get_bool(sm, EAPOL_portEnabled))
1084 SM_ENTER_GLOBAL(EAP, INITIALIZE);
1085 else if (!eapol_get_bool(sm, EAPOL_portEnabled) || sm->force_disabled)
1086 SM_ENTER_GLOBAL(EAP, DISABLED);
1087 else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
1088 /* RFC 4137 does not place any limit on number of EAP messages
1089 * in an authentication session. However, some error cases have
1090 * ended up in a state were EAP messages were sent between the
1091 * peer and server in a loop (e.g., TLS ACK frame in both
1092 * direction). Since this is quite undesired outcome, limit the
1093 * total number of EAP round-trips and abort authentication if
1094 * this limit is exceeded.
1095 */
1096 if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
1097 wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: more than %d "
1098 "authentication rounds - abort",
1099 EAP_MAX_AUTH_ROUNDS);
1100 sm->num_rounds++;
1101 SM_ENTER_GLOBAL(EAP, FAILURE);
1102 }
1103 } else {
1104 /* Local transitions */
1105 eap_peer_sm_step_local(sm);
1106 }
1107}
1108
1109
1110static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
1111 EapType method)
1112{
1113 if (!eap_allowed_method(sm, vendor, method)) {
1114 wpa_printf(MSG_DEBUG, "EAP: configuration does not allow: "
1115 "vendor %u method %u", vendor, method);
1116 return FALSE;
1117 }
1118 if (eap_peer_get_eap_method(vendor, method))
1119 return TRUE;
1120 wpa_printf(MSG_DEBUG, "EAP: not included in build: "
1121 "vendor %u method %u", vendor, method);
1122 return FALSE;
1123}
1124
1125
1126static struct wpabuf * eap_sm_build_expanded_nak(
1127 struct eap_sm *sm, int id, const struct eap_method *methods,
1128 size_t count)
1129{
1130 struct wpabuf *resp;
1131 int found = 0;
1132 const struct eap_method *m;
1133
1134 wpa_printf(MSG_DEBUG, "EAP: Building expanded EAP-Nak");
1135
1136 /* RFC 3748 - 5.3.2: Expanded Nak */
1137 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_EXPANDED,
1138 8 + 8 * (count + 1), EAP_CODE_RESPONSE, id);
1139 if (resp == NULL)
1140 return NULL;
1141
1142 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
1143 wpabuf_put_be32(resp, EAP_TYPE_NAK);
1144
1145 for (m = methods; m; m = m->next) {
1146 if (sm->reqVendor == m->vendor &&
1147 sm->reqVendorMethod == m->method)
1148 continue; /* do not allow the current method again */
1149 if (eap_allowed_method(sm, m->vendor, m->method)) {
1150 wpa_printf(MSG_DEBUG, "EAP: allowed type: "
1151 "vendor=%u method=%u",
1152 m->vendor, m->method);
1153 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
1154 wpabuf_put_be24(resp, m->vendor);
1155 wpabuf_put_be32(resp, m->method);
1156
1157 found++;
1158 }
1159 }
1160 if (!found) {
1161 wpa_printf(MSG_DEBUG, "EAP: no more allowed methods");
1162 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
1163 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
1164 wpabuf_put_be32(resp, EAP_TYPE_NONE);
1165 }
1166
1167 eap_update_len(resp);
1168
1169 return resp;
1170}
1171
1172
1173static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id)
1174{
1175 struct wpabuf *resp;
1176 u8 *start;
1177 int found = 0, expanded_found = 0;
1178 size_t count;
1179 const struct eap_method *methods, *m;
1180
1181 wpa_printf(MSG_DEBUG, "EAP: Building EAP-Nak (requested type %u "
1182 "vendor=%u method=%u not allowed)", sm->reqMethod,
1183 sm->reqVendor, sm->reqVendorMethod);
1184 methods = eap_peer_get_methods(&count);
1185 if (methods == NULL)
1186 return NULL;
1187 if (sm->reqMethod == EAP_TYPE_EXPANDED)
1188 return eap_sm_build_expanded_nak(sm, id, methods, count);
1189
1190 /* RFC 3748 - 5.3.1: Legacy Nak */
1191 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK,
1192 sizeof(struct eap_hdr) + 1 + count + 1,
1193 EAP_CODE_RESPONSE, id);
1194 if (resp == NULL)
1195 return NULL;
1196
1197 start = wpabuf_put(resp, 0);
1198 for (m = methods; m; m = m->next) {
1199 if (m->vendor == EAP_VENDOR_IETF && m->method == sm->reqMethod)
1200 continue; /* do not allow the current method again */
1201 if (eap_allowed_method(sm, m->vendor, m->method)) {
1202 if (m->vendor != EAP_VENDOR_IETF) {
1203 if (expanded_found)
1204 continue;
1205 expanded_found = 1;
1206 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
1207 } else
1208 wpabuf_put_u8(resp, m->method);
1209 found++;
1210 }
1211 }
1212 if (!found)
1213 wpabuf_put_u8(resp, EAP_TYPE_NONE);
1214 wpa_hexdump(MSG_DEBUG, "EAP: allowed methods", start, found);
1215
1216 eap_update_len(resp);
1217
1218 return resp;
1219}
1220
1221
1222static void eap_sm_processIdentity(struct eap_sm *sm, const struct wpabuf *req)
1223{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001224 const u8 *pos;
1225 size_t msg_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226
1227 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
1228 "EAP authentication started");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001229 eap_notify_status(sm, "started", "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001231 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, req,
1232 &msg_len);
1233 if (pos == NULL)
1234 return;
1235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 /*
1237 * RFC 3748 - 5.1: Identity
1238 * Data field may contain a displayable message in UTF-8. If this
1239 * includes NUL-character, only the data before that should be
1240 * displayed. Some EAP implementasitons may piggy-back additional
1241 * options after the NUL.
1242 */
1243 /* TODO: could save displayable message so that it can be shown to the
1244 * user in case of interaction is required */
1245 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Identity data",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001246 pos, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247}
1248
1249
1250#ifdef PCSC_FUNCS
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001251
1252/*
1253 * Rules for figuring out MNC length based on IMSI for SIM cards that do not
1254 * include MNC length field.
1255 */
1256static int mnc_len_from_imsi(const char *imsi)
1257{
1258 char mcc_str[4];
1259 unsigned int mcc;
1260
1261 os_memcpy(mcc_str, imsi, 3);
1262 mcc_str[3] = '\0';
1263 mcc = atoi(mcc_str);
1264
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001265 if (mcc == 228)
1266 return 2; /* Networks in Switzerland use 2-digit MNC */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001267 if (mcc == 244)
1268 return 2; /* Networks in Finland use 2-digit MNC */
1269
1270 return -1;
1271}
1272
1273
1274static int eap_sm_append_3gpp_realm(struct eap_sm *sm, char *imsi,
1275 size_t max_len, size_t *imsi_len)
1276{
1277 int mnc_len;
1278 char *pos, mnc[4];
1279
1280 if (*imsi_len + 36 > max_len) {
1281 wpa_printf(MSG_WARNING, "No room for realm in IMSI buffer");
1282 return -1;
1283 }
1284
1285 /* MNC (2 or 3 digits) */
1286 mnc_len = scard_get_mnc_len(sm->scard_ctx);
1287 if (mnc_len < 0)
1288 mnc_len = mnc_len_from_imsi(imsi);
1289 if (mnc_len < 0) {
1290 wpa_printf(MSG_INFO, "Failed to get MNC length from (U)SIM "
1291 "assuming 3");
1292 mnc_len = 3;
1293 }
1294
1295 if (mnc_len == 2) {
1296 mnc[0] = '0';
1297 mnc[1] = imsi[3];
1298 mnc[2] = imsi[4];
1299 } else if (mnc_len == 3) {
1300 mnc[0] = imsi[3];
1301 mnc[1] = imsi[4];
1302 mnc[2] = imsi[5];
1303 }
1304 mnc[3] = '\0';
1305
1306 pos = imsi + *imsi_len;
1307 pos += os_snprintf(pos, imsi + max_len - pos,
1308 "@wlan.mnc%s.mcc%c%c%c.3gppnetwork.org",
1309 mnc, imsi[0], imsi[1], imsi[2]);
1310 *imsi_len = pos - imsi;
1311
1312 return 0;
1313}
1314
1315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316static int eap_sm_imsi_identity(struct eap_sm *sm,
1317 struct eap_peer_config *conf)
1318{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001319 enum { EAP_SM_SIM, EAP_SM_AKA, EAP_SM_AKA_PRIME } method = EAP_SM_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 char imsi[100];
1321 size_t imsi_len;
1322 struct eap_method_type *m = conf->eap_methods;
1323 int i;
1324
1325 imsi_len = sizeof(imsi);
1326 if (scard_get_imsi(sm->scard_ctx, imsi, &imsi_len)) {
1327 wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM");
1328 return -1;
1329 }
1330
1331 wpa_hexdump_ascii(MSG_DEBUG, "IMSI", (u8 *) imsi, imsi_len);
1332
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001333 if (imsi_len < 7) {
1334 wpa_printf(MSG_WARNING, "Too short IMSI for SIM identity");
1335 return -1;
1336 }
1337
1338 if (eap_sm_append_3gpp_realm(sm, imsi, sizeof(imsi), &imsi_len) < 0) {
1339 wpa_printf(MSG_WARNING, "Could not add realm to SIM identity");
1340 return -1;
1341 }
1342 wpa_hexdump_ascii(MSG_DEBUG, "IMSI + realm", (u8 *) imsi, imsi_len);
1343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 for (i = 0; m && (m[i].vendor != EAP_VENDOR_IETF ||
1345 m[i].method != EAP_TYPE_NONE); i++) {
1346 if (m[i].vendor == EAP_VENDOR_IETF &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07001347 m[i].method == EAP_TYPE_AKA_PRIME) {
1348 method = EAP_SM_AKA_PRIME;
1349 break;
1350 }
1351
1352 if (m[i].vendor == EAP_VENDOR_IETF &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 m[i].method == EAP_TYPE_AKA) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001354 method = EAP_SM_AKA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 break;
1356 }
1357 }
1358
1359 os_free(conf->identity);
1360 conf->identity = os_malloc(1 + imsi_len);
1361 if (conf->identity == NULL) {
1362 wpa_printf(MSG_WARNING, "Failed to allocate buffer for "
1363 "IMSI-based identity");
1364 return -1;
1365 }
1366
Dmitry Shmidt04949592012-07-19 12:16:46 -07001367 switch (method) {
1368 case EAP_SM_SIM:
1369 conf->identity[0] = '1';
1370 break;
1371 case EAP_SM_AKA:
1372 conf->identity[0] = '0';
1373 break;
1374 case EAP_SM_AKA_PRIME:
1375 conf->identity[0] = '6';
1376 break;
1377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 os_memcpy(conf->identity + 1, imsi, imsi_len);
1379 conf->identity_len = 1 + imsi_len;
1380
1381 return 0;
1382}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384#endif /* PCSC_FUNCS */
1385
1386
1387static int eap_sm_set_scard_pin(struct eap_sm *sm,
1388 struct eap_peer_config *conf)
1389{
1390#ifdef PCSC_FUNCS
1391 if (scard_set_pin(sm->scard_ctx, conf->pin)) {
1392 /*
1393 * Make sure the same PIN is not tried again in order to avoid
1394 * blocking SIM.
1395 */
1396 os_free(conf->pin);
1397 conf->pin = NULL;
1398
1399 wpa_printf(MSG_WARNING, "PIN validation failed");
1400 eap_sm_request_pin(sm);
1401 return -1;
1402 }
1403 return 0;
1404#else /* PCSC_FUNCS */
1405 return -1;
1406#endif /* PCSC_FUNCS */
1407}
1408
1409static int eap_sm_get_scard_identity(struct eap_sm *sm,
1410 struct eap_peer_config *conf)
1411{
1412#ifdef PCSC_FUNCS
1413 if (eap_sm_set_scard_pin(sm, conf))
1414 return -1;
1415
1416 return eap_sm_imsi_identity(sm, conf);
1417#else /* PCSC_FUNCS */
1418 return -1;
1419#endif /* PCSC_FUNCS */
1420}
1421
1422
1423/**
1424 * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
1425 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1426 * @id: EAP identifier for the packet
1427 * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
1428 * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
1429 * failure
1430 *
1431 * This function allocates and builds an EAP-Identity/Response packet for the
1432 * current network. The caller is responsible for freeing the returned data.
1433 */
1434struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted)
1435{
1436 struct eap_peer_config *config = eap_get_config(sm);
1437 struct wpabuf *resp;
1438 const u8 *identity;
1439 size_t identity_len;
1440
1441 if (config == NULL) {
1442 wpa_printf(MSG_WARNING, "EAP: buildIdentity: configuration "
1443 "was not available");
1444 return NULL;
1445 }
1446
1447 if (sm->m && sm->m->get_identity &&
1448 (identity = sm->m->get_identity(sm, sm->eap_method_priv,
1449 &identity_len)) != NULL) {
1450 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth "
1451 "identity", identity, identity_len);
1452 } else if (!encrypted && config->anonymous_identity) {
1453 identity = config->anonymous_identity;
1454 identity_len = config->anonymous_identity_len;
1455 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity",
1456 identity, identity_len);
1457 } else {
1458 identity = config->identity;
1459 identity_len = config->identity_len;
1460 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity",
1461 identity, identity_len);
1462 }
1463
1464 if (identity == NULL) {
1465 wpa_printf(MSG_WARNING, "EAP: buildIdentity: identity "
1466 "configuration was not available");
1467 if (config->pcsc) {
1468 if (eap_sm_get_scard_identity(sm, config) < 0)
1469 return NULL;
1470 identity = config->identity;
1471 identity_len = config->identity_len;
1472 wpa_hexdump_ascii(MSG_DEBUG, "permanent identity from "
1473 "IMSI", identity, identity_len);
1474 } else {
1475 eap_sm_request_identity(sm);
1476 return NULL;
1477 }
1478 } else if (config->pcsc) {
1479 if (eap_sm_set_scard_pin(sm, config) < 0)
1480 return NULL;
1481 }
1482
1483 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, identity_len,
1484 EAP_CODE_RESPONSE, id);
1485 if (resp == NULL)
1486 return NULL;
1487
1488 wpabuf_put_data(resp, identity, identity_len);
1489
1490 return resp;
1491}
1492
1493
1494static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req)
1495{
1496 const u8 *pos;
1497 char *msg;
1498 size_t i, msg_len;
1499
1500 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, req,
1501 &msg_len);
1502 if (pos == NULL)
1503 return;
1504 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Notification data",
1505 pos, msg_len);
1506
1507 msg = os_malloc(msg_len + 1);
1508 if (msg == NULL)
1509 return;
1510 for (i = 0; i < msg_len; i++)
1511 msg[i] = isprint(pos[i]) ? (char) pos[i] : '_';
1512 msg[msg_len] = '\0';
1513 wpa_msg(sm->msg_ctx, MSG_INFO, "%s%s",
1514 WPA_EVENT_EAP_NOTIFICATION, msg);
1515 os_free(msg);
1516}
1517
1518
1519static struct wpabuf * eap_sm_buildNotify(int id)
1520{
1521 struct wpabuf *resp;
1522
1523 wpa_printf(MSG_DEBUG, "EAP: Generating EAP-Response Notification");
1524 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, 0,
1525 EAP_CODE_RESPONSE, id);
1526 if (resp == NULL)
1527 return NULL;
1528
1529 return resp;
1530}
1531
1532
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001533static void eap_peer_initiate(struct eap_sm *sm, const struct eap_hdr *hdr,
1534 size_t len)
1535{
1536#ifdef CONFIG_ERP
1537 const u8 *pos = (const u8 *) (hdr + 1);
1538 const u8 *end = ((const u8 *) hdr) + len;
1539 struct erp_tlvs parse;
1540
1541 if (len < sizeof(*hdr) + 1) {
1542 wpa_printf(MSG_DEBUG, "EAP: Ignored too short EAP-Initiate");
1543 return;
1544 }
1545
1546 if (*pos != EAP_ERP_TYPE_REAUTH_START) {
1547 wpa_printf(MSG_DEBUG,
1548 "EAP: Ignored unexpected EAP-Initiate Type=%u",
1549 *pos);
1550 return;
1551 }
1552
1553 pos++;
1554 if (pos >= end) {
1555 wpa_printf(MSG_DEBUG,
1556 "EAP: Too short EAP-Initiate/Re-auth-Start");
1557 return;
1558 }
1559 pos++; /* Reserved */
1560 wpa_hexdump(MSG_DEBUG, "EAP: EAP-Initiate/Re-auth-Start TVs/TLVs",
1561 pos, end - pos);
1562
1563 if (erp_parse_tlvs(pos, end, &parse, 0) < 0)
1564 goto invalid;
1565
1566 if (parse.domain) {
1567 wpa_hexdump_ascii(MSG_DEBUG,
1568 "EAP: EAP-Initiate/Re-auth-Start - Domain name",
1569 parse.domain, parse.domain_len);
1570 /* TODO: Derivation of domain specific keys for local ER */
1571 }
1572
1573 if (eap_peer_erp_reauth_start(sm, hdr, len) == 0)
1574 return;
1575
1576invalid:
1577#endif /* CONFIG_ERP */
1578 wpa_printf(MSG_DEBUG,
1579 "EAP: EAP-Initiate/Re-auth-Start - No suitable ERP keys available - try to start full EAP authentication");
1580 eapol_set_bool(sm, EAPOL_eapTriggerStart, TRUE);
1581}
1582
1583
1584static void eap_peer_finish(struct eap_sm *sm, const struct eap_hdr *hdr,
1585 size_t len)
1586{
1587#ifdef CONFIG_ERP
1588 const u8 *pos = (const u8 *) (hdr + 1);
1589 const u8 *end = ((const u8 *) hdr) + len;
1590 const u8 *start;
1591 struct erp_tlvs parse;
1592 u8 flags;
1593 u16 seq;
1594 u8 hash[SHA256_MAC_LEN];
1595 size_t hash_len;
1596 struct eap_erp_key *erp;
1597 int max_len;
1598 char nai[254];
1599 u8 seed[4];
1600 int auth_tag_ok = 0;
1601
1602 if (len < sizeof(*hdr) + 1) {
1603 wpa_printf(MSG_DEBUG, "EAP: Ignored too short EAP-Finish");
1604 return;
1605 }
1606
1607 if (*pos != EAP_ERP_TYPE_REAUTH) {
1608 wpa_printf(MSG_DEBUG,
1609 "EAP: Ignored unexpected EAP-Finish Type=%u", *pos);
1610 return;
1611 }
1612
1613 if (len < sizeof(*hdr) + 4) {
1614 wpa_printf(MSG_DEBUG,
1615 "EAP: Ignored too short EAP-Finish/Re-auth");
1616 return;
1617 }
1618
1619 pos++;
1620 flags = *pos++;
1621 seq = WPA_GET_BE16(pos);
1622 pos += 2;
1623 wpa_printf(MSG_DEBUG, "EAP: Flags=0x%x SEQ=%u", flags, seq);
1624
1625 if (seq != sm->erp_seq) {
1626 wpa_printf(MSG_DEBUG,
1627 "EAP: Unexpected EAP-Finish/Re-auth SEQ=%u", seq);
1628 return;
1629 }
1630
1631 /*
1632 * Parse TVs/TLVs. Since we do not yet know the length of the
1633 * Authentication Tag, stop parsing if an unknown TV/TLV is seen and
1634 * just try to find the keyName-NAI first so that we can check the
1635 * Authentication Tag.
1636 */
1637 if (erp_parse_tlvs(pos, end, &parse, 1) < 0)
1638 return;
1639
1640 if (!parse.keyname) {
1641 wpa_printf(MSG_DEBUG,
1642 "EAP: No keyName-NAI in EAP-Finish/Re-auth Packet");
1643 return;
1644 }
1645
1646 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Finish/Re-auth - keyName-NAI",
1647 parse.keyname, parse.keyname_len);
1648 if (parse.keyname_len > 253) {
1649 wpa_printf(MSG_DEBUG,
1650 "EAP: Too long keyName-NAI in EAP-Finish/Re-auth");
1651 return;
1652 }
1653 os_memcpy(nai, parse.keyname, parse.keyname_len);
1654 nai[parse.keyname_len] = '\0';
1655
1656 erp = eap_erp_get_key_nai(sm, nai);
1657 if (!erp) {
1658 wpa_printf(MSG_DEBUG, "EAP: No matching ERP key found for %s",
1659 nai);
1660 return;
1661 }
1662
1663 /* Is there enough room for Cryptosuite and Authentication Tag? */
1664 start = parse.keyname + parse.keyname_len;
1665 max_len = end - start;
1666 hash_len = 16;
1667 if (max_len < 1 + (int) hash_len) {
1668 wpa_printf(MSG_DEBUG,
1669 "EAP: Not enough room for Authentication Tag");
1670 if (flags & 0x80)
1671 goto no_auth_tag;
1672 return;
1673 }
1674 if (end[-17] != EAP_ERP_CS_HMAC_SHA256_128) {
1675 wpa_printf(MSG_DEBUG, "EAP: Different Cryptosuite used");
1676 if (flags & 0x80)
1677 goto no_auth_tag;
1678 return;
1679 }
1680
1681 if (hmac_sha256(erp->rIK, erp->rIK_len, (const u8 *) hdr,
1682 end - ((const u8 *) hdr) - hash_len, hash) < 0)
1683 return;
1684 if (os_memcmp(end - hash_len, hash, hash_len) != 0) {
1685 wpa_printf(MSG_DEBUG,
1686 "EAP: Authentication Tag mismatch");
1687 return;
1688 }
1689 auth_tag_ok = 1;
1690 end -= 1 + hash_len;
1691
1692no_auth_tag:
1693 /*
1694 * Parse TVs/TLVs again now that we know the exact part of the buffer
1695 * that contains them.
1696 */
1697 wpa_hexdump(MSG_DEBUG, "EAP: EAP-Finish/Re-Auth TVs/TLVs",
1698 pos, end - pos);
1699 if (erp_parse_tlvs(pos, end, &parse, 0) < 0)
1700 return;
1701
1702 if (flags & 0x80 || !auth_tag_ok) {
1703 wpa_printf(MSG_DEBUG,
1704 "EAP: EAP-Finish/Re-auth indicated failure");
1705 eapol_set_bool(sm, EAPOL_eapFail, TRUE);
1706 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
1707 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
1708 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
1709 "EAP authentication failed");
1710 sm->prev_failure = 1;
1711 wpa_printf(MSG_DEBUG,
1712 "EAP: Drop ERP key to try full authentication on next attempt");
1713 eap_peer_erp_free_key(erp);
1714 return;
1715 }
1716
1717 eap_sm_free_key(sm);
1718 sm->eapKeyDataLen = 0;
1719 sm->eapKeyData = os_malloc(erp->rRK_len);
1720 if (!sm->eapKeyData)
1721 return;
1722 sm->eapKeyDataLen = erp->rRK_len;
1723
1724 WPA_PUT_BE16(seed, seq);
1725 WPA_PUT_BE16(&seed[2], erp->rRK_len);
1726 if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
1727 "Re-authentication Master Session Key@ietf.org",
1728 seed, sizeof(seed),
1729 sm->eapKeyData, erp->rRK_len) < 0) {
1730 wpa_printf(MSG_DEBUG, "EAP: Could not derive rMSK for ERP");
1731 eap_sm_free_key(sm);
1732 return;
1733 }
1734 wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rMSK",
1735 sm->eapKeyData, sm->eapKeyDataLen);
1736 sm->eapKeyAvailable = TRUE;
1737 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
1738 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
1739 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
1740 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
1741 "EAP re-authentication completed successfully");
1742#endif /* CONFIG_ERP */
1743}
1744
1745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req)
1747{
1748 const struct eap_hdr *hdr;
1749 size_t plen;
1750 const u8 *pos;
1751
1752 sm->rxReq = sm->rxResp = sm->rxSuccess = sm->rxFailure = FALSE;
1753 sm->reqId = 0;
1754 sm->reqMethod = EAP_TYPE_NONE;
1755 sm->reqVendor = EAP_VENDOR_IETF;
1756 sm->reqVendorMethod = EAP_TYPE_NONE;
1757
1758 if (req == NULL || wpabuf_len(req) < sizeof(*hdr))
1759 return;
1760
1761 hdr = wpabuf_head(req);
1762 plen = be_to_host16(hdr->length);
1763 if (plen > wpabuf_len(req)) {
1764 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
1765 "(len=%lu plen=%lu)",
1766 (unsigned long) wpabuf_len(req),
1767 (unsigned long) plen);
1768 return;
1769 }
1770
1771 sm->reqId = hdr->identifier;
1772
1773 if (sm->workaround) {
1774 const u8 *addr[1];
1775 addr[0] = wpabuf_head(req);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001776 sha1_vector(1, addr, &plen, sm->req_sha1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 }
1778
1779 switch (hdr->code) {
1780 case EAP_CODE_REQUEST:
1781 if (plen < sizeof(*hdr) + 1) {
1782 wpa_printf(MSG_DEBUG, "EAP: Too short EAP-Request - "
1783 "no Type field");
1784 return;
1785 }
1786 sm->rxReq = TRUE;
1787 pos = (const u8 *) (hdr + 1);
1788 sm->reqMethod = *pos++;
1789 if (sm->reqMethod == EAP_TYPE_EXPANDED) {
1790 if (plen < sizeof(*hdr) + 8) {
1791 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
1792 "expanded EAP-Packet (plen=%lu)",
1793 (unsigned long) plen);
1794 return;
1795 }
1796 sm->reqVendor = WPA_GET_BE24(pos);
1797 pos += 3;
1798 sm->reqVendorMethod = WPA_GET_BE32(pos);
1799 }
1800 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Request id=%d "
1801 "method=%u vendor=%u vendorMethod=%u",
1802 sm->reqId, sm->reqMethod, sm->reqVendor,
1803 sm->reqVendorMethod);
1804 break;
1805 case EAP_CODE_RESPONSE:
1806 if (sm->selectedMethod == EAP_TYPE_LEAP) {
1807 /*
1808 * LEAP differs from RFC 4137 by using reversed roles
1809 * for mutual authentication and because of this, we
1810 * need to accept EAP-Response frames if LEAP is used.
1811 */
1812 if (plen < sizeof(*hdr) + 1) {
1813 wpa_printf(MSG_DEBUG, "EAP: Too short "
1814 "EAP-Response - no Type field");
1815 return;
1816 }
1817 sm->rxResp = TRUE;
1818 pos = (const u8 *) (hdr + 1);
1819 sm->reqMethod = *pos;
1820 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Response for "
1821 "LEAP method=%d id=%d",
1822 sm->reqMethod, sm->reqId);
1823 break;
1824 }
1825 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Response");
1826 break;
1827 case EAP_CODE_SUCCESS:
1828 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Success");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001829 eap_notify_status(sm, "completion", "success");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830 sm->rxSuccess = TRUE;
1831 break;
1832 case EAP_CODE_FAILURE:
1833 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Failure");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001834 eap_notify_status(sm, "completion", "failure");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 sm->rxFailure = TRUE;
1836 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001837 case EAP_CODE_INITIATE:
1838 eap_peer_initiate(sm, hdr, plen);
1839 break;
1840 case EAP_CODE_FINISH:
1841 eap_peer_finish(sm, hdr, plen);
1842 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 default:
1844 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Packet with unknown "
1845 "code %d", hdr->code);
1846 break;
1847 }
1848}
1849
1850
1851static void eap_peer_sm_tls_event(void *ctx, enum tls_event ev,
1852 union tls_event_data *data)
1853{
1854 struct eap_sm *sm = ctx;
1855 char *hash_hex = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856
1857 switch (ev) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001858 case TLS_CERT_CHAIN_SUCCESS:
1859 eap_notify_status(sm, "remote certificate verification",
1860 "success");
1861 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862 case TLS_CERT_CHAIN_FAILURE:
1863 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_TLS_CERT_ERROR
1864 "reason=%d depth=%d subject='%s' err='%s'",
1865 data->cert_fail.reason,
1866 data->cert_fail.depth,
1867 data->cert_fail.subject,
1868 data->cert_fail.reason_txt);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001869 eap_notify_status(sm, "remote certificate verification",
1870 data->cert_fail.reason_txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871 break;
1872 case TLS_PEER_CERTIFICATE:
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001873 if (!sm->eapol_cb->notify_cert)
1874 break;
1875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 if (data->peer_cert.hash) {
1877 size_t len = data->peer_cert.hash_len * 2 + 1;
1878 hash_hex = os_malloc(len);
1879 if (hash_hex) {
1880 wpa_snprintf_hex(hash_hex, len,
1881 data->peer_cert.hash,
1882 data->peer_cert.hash_len);
1883 }
1884 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001886 sm->eapol_cb->notify_cert(sm->eapol_ctx,
1887 data->peer_cert.depth,
1888 data->peer_cert.subject,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001889 data->peer_cert.altsubject,
1890 data->peer_cert.num_altsubject,
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001891 hash_hex, data->peer_cert.cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001893 case TLS_ALERT:
1894 if (data->alert.is_local)
1895 eap_notify_status(sm, "local TLS alert",
1896 data->alert.description);
1897 else
1898 eap_notify_status(sm, "remote TLS alert",
1899 data->alert.description);
1900 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001901 }
1902
1903 os_free(hash_hex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904}
1905
1906
1907/**
1908 * eap_peer_sm_init - Allocate and initialize EAP peer state machine
1909 * @eapol_ctx: Context data to be used with eapol_cb calls
1910 * @eapol_cb: Pointer to EAPOL callback functions
1911 * @msg_ctx: Context data for wpa_msg() calls
1912 * @conf: EAP configuration
1913 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
1914 *
1915 * This function allocates and initializes an EAP state machine. In addition,
1916 * this initializes TLS library for the new EAP state machine. eapol_cb pointer
1917 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
1918 * state machine. Consequently, the caller must make sure that this data
1919 * structure remains alive while the EAP state machine is active.
1920 */
1921struct eap_sm * eap_peer_sm_init(void *eapol_ctx,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001922 const struct eapol_callbacks *eapol_cb,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923 void *msg_ctx, struct eap_config *conf)
1924{
1925 struct eap_sm *sm;
1926 struct tls_config tlsconf;
1927
1928 sm = os_zalloc(sizeof(*sm));
1929 if (sm == NULL)
1930 return NULL;
1931 sm->eapol_ctx = eapol_ctx;
1932 sm->eapol_cb = eapol_cb;
1933 sm->msg_ctx = msg_ctx;
1934 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
1935 sm->wps = conf->wps;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001936 dl_list_init(&sm->erp_keys);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937
1938 os_memset(&tlsconf, 0, sizeof(tlsconf));
1939 tlsconf.opensc_engine_path = conf->opensc_engine_path;
1940 tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path;
1941 tlsconf.pkcs11_module_path = conf->pkcs11_module_path;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001942 tlsconf.openssl_ciphers = conf->openssl_ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943#ifdef CONFIG_FIPS
1944 tlsconf.fips_mode = 1;
1945#endif /* CONFIG_FIPS */
1946 tlsconf.event_cb = eap_peer_sm_tls_event;
1947 tlsconf.cb_ctx = sm;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001948 tlsconf.cert_in_cb = conf->cert_in_cb;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949 sm->ssl_ctx = tls_init(&tlsconf);
1950 if (sm->ssl_ctx == NULL) {
1951 wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS "
1952 "context.");
1953 os_free(sm);
1954 return NULL;
1955 }
1956
Dmitry Shmidt04949592012-07-19 12:16:46 -07001957 sm->ssl_ctx2 = tls_init(&tlsconf);
1958 if (sm->ssl_ctx2 == NULL) {
1959 wpa_printf(MSG_INFO, "SSL: Failed to initialize TLS "
1960 "context (2).");
1961 /* Run without separate TLS context within TLS tunnel */
1962 }
1963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 return sm;
1965}
1966
1967
1968/**
1969 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
1970 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1971 *
1972 * This function deinitializes EAP state machine and frees all allocated
1973 * resources.
1974 */
1975void eap_peer_sm_deinit(struct eap_sm *sm)
1976{
1977 if (sm == NULL)
1978 return;
1979 eap_deinit_prev_method(sm, "EAP deinit");
1980 eap_sm_abort(sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001981 if (sm->ssl_ctx2)
1982 tls_deinit(sm->ssl_ctx2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983 tls_deinit(sm->ssl_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001984 eap_peer_erp_free_keys(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 os_free(sm);
1986}
1987
1988
1989/**
1990 * eap_peer_sm_step - Step EAP peer state machine
1991 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1992 * Returns: 1 if EAP state was changed or 0 if not
1993 *
1994 * This function advances EAP state machine to a new state to match with the
1995 * current variables. This should be called whenever variables used by the EAP
1996 * state machine have changed.
1997 */
1998int eap_peer_sm_step(struct eap_sm *sm)
1999{
2000 int res = 0;
2001 do {
2002 sm->changed = FALSE;
2003 SM_STEP_RUN(EAP);
2004 if (sm->changed)
2005 res = 1;
2006 } while (sm->changed);
2007 return res;
2008}
2009
2010
2011/**
2012 * eap_sm_abort - Abort EAP authentication
2013 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2014 *
2015 * Release system resources that have been allocated for the authentication
2016 * session without fully deinitializing the EAP state machine.
2017 */
2018void eap_sm_abort(struct eap_sm *sm)
2019{
2020 wpabuf_free(sm->lastRespData);
2021 sm->lastRespData = NULL;
2022 wpabuf_free(sm->eapRespData);
2023 sm->eapRespData = NULL;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002024 eap_sm_free_key(sm);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002025 os_free(sm->eapSessionId);
2026 sm->eapSessionId = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027
2028 /* This is not clearly specified in the EAP statemachines draft, but
2029 * it seems necessary to make sure that some of the EAPOL variables get
2030 * cleared for the next authentication. */
2031 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
2032}
2033
2034
2035#ifdef CONFIG_CTRL_IFACE
2036static const char * eap_sm_state_txt(int state)
2037{
2038 switch (state) {
2039 case EAP_INITIALIZE:
2040 return "INITIALIZE";
2041 case EAP_DISABLED:
2042 return "DISABLED";
2043 case EAP_IDLE:
2044 return "IDLE";
2045 case EAP_RECEIVED:
2046 return "RECEIVED";
2047 case EAP_GET_METHOD:
2048 return "GET_METHOD";
2049 case EAP_METHOD:
2050 return "METHOD";
2051 case EAP_SEND_RESPONSE:
2052 return "SEND_RESPONSE";
2053 case EAP_DISCARD:
2054 return "DISCARD";
2055 case EAP_IDENTITY:
2056 return "IDENTITY";
2057 case EAP_NOTIFICATION:
2058 return "NOTIFICATION";
2059 case EAP_RETRANSMIT:
2060 return "RETRANSMIT";
2061 case EAP_SUCCESS:
2062 return "SUCCESS";
2063 case EAP_FAILURE:
2064 return "FAILURE";
2065 default:
2066 return "UNKNOWN";
2067 }
2068}
2069#endif /* CONFIG_CTRL_IFACE */
2070
2071
2072#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
2073static const char * eap_sm_method_state_txt(EapMethodState state)
2074{
2075 switch (state) {
2076 case METHOD_NONE:
2077 return "NONE";
2078 case METHOD_INIT:
2079 return "INIT";
2080 case METHOD_CONT:
2081 return "CONT";
2082 case METHOD_MAY_CONT:
2083 return "MAY_CONT";
2084 case METHOD_DONE:
2085 return "DONE";
2086 default:
2087 return "UNKNOWN";
2088 }
2089}
2090
2091
2092static const char * eap_sm_decision_txt(EapDecision decision)
2093{
2094 switch (decision) {
2095 case DECISION_FAIL:
2096 return "FAIL";
2097 case DECISION_COND_SUCC:
2098 return "COND_SUCC";
2099 case DECISION_UNCOND_SUCC:
2100 return "UNCOND_SUCC";
2101 default:
2102 return "UNKNOWN";
2103 }
2104}
2105#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
2106
2107
2108#ifdef CONFIG_CTRL_IFACE
2109
2110/**
2111 * eap_sm_get_status - Get EAP state machine status
2112 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2113 * @buf: Buffer for status information
2114 * @buflen: Maximum buffer length
2115 * @verbose: Whether to include verbose status information
2116 * Returns: Number of bytes written to buf.
2117 *
2118 * Query EAP state machine for status information. This function fills in a
2119 * text area with current status information from the EAPOL state machine. If
2120 * the buffer (buf) is not large enough, status information will be truncated
2121 * to fit the buffer.
2122 */
2123int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose)
2124{
2125 int len, ret;
2126
2127 if (sm == NULL)
2128 return 0;
2129
2130 len = os_snprintf(buf, buflen,
2131 "EAP state=%s\n",
2132 eap_sm_state_txt(sm->EAP_state));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002133 if (os_snprintf_error(buflen, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 return 0;
2135
2136 if (sm->selectedMethod != EAP_TYPE_NONE) {
2137 const char *name;
2138 if (sm->m) {
2139 name = sm->m->name;
2140 } else {
2141 const struct eap_method *m =
2142 eap_peer_get_eap_method(EAP_VENDOR_IETF,
2143 sm->selectedMethod);
2144 if (m)
2145 name = m->name;
2146 else
2147 name = "?";
2148 }
2149 ret = os_snprintf(buf + len, buflen - len,
2150 "selectedMethod=%d (EAP-%s)\n",
2151 sm->selectedMethod, name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002152 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153 return len;
2154 len += ret;
2155
2156 if (sm->m && sm->m->get_status) {
2157 len += sm->m->get_status(sm, sm->eap_method_priv,
2158 buf + len, buflen - len,
2159 verbose);
2160 }
2161 }
2162
2163 if (verbose) {
2164 ret = os_snprintf(buf + len, buflen - len,
2165 "reqMethod=%d\n"
2166 "methodState=%s\n"
2167 "decision=%s\n"
2168 "ClientTimeout=%d\n",
2169 sm->reqMethod,
2170 eap_sm_method_state_txt(sm->methodState),
2171 eap_sm_decision_txt(sm->decision),
2172 sm->ClientTimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002173 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174 return len;
2175 len += ret;
2176 }
2177
2178 return len;
2179}
2180#endif /* CONFIG_CTRL_IFACE */
2181
2182
2183#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002184static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 const char *msg, size_t msglen)
2186{
2187 struct eap_peer_config *config;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002188 const char *txt = NULL;
2189 char *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190
2191 if (sm == NULL)
2192 return;
2193 config = eap_get_config(sm);
2194 if (config == NULL)
2195 return;
2196
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002197 switch (field) {
2198 case WPA_CTRL_REQ_EAP_IDENTITY:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 config->pending_req_identity++;
2200 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002201 case WPA_CTRL_REQ_EAP_PASSWORD:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 config->pending_req_password++;
2203 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002204 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 config->pending_req_new_password++;
2206 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002207 case WPA_CTRL_REQ_EAP_PIN:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 config->pending_req_pin++;
2209 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002210 case WPA_CTRL_REQ_EAP_OTP:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211 if (msg) {
2212 tmp = os_malloc(msglen + 3);
2213 if (tmp == NULL)
2214 return;
2215 tmp[0] = '[';
2216 os_memcpy(tmp + 1, msg, msglen);
2217 tmp[msglen + 1] = ']';
2218 tmp[msglen + 2] = '\0';
2219 txt = tmp;
2220 os_free(config->pending_req_otp);
2221 config->pending_req_otp = tmp;
2222 config->pending_req_otp_len = msglen + 3;
2223 } else {
2224 if (config->pending_req_otp == NULL)
2225 return;
2226 txt = config->pending_req_otp;
2227 }
2228 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002229 case WPA_CTRL_REQ_EAP_PASSPHRASE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 config->pending_req_passphrase++;
2231 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002232 case WPA_CTRL_REQ_SIM:
2233 txt = msg;
2234 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 default:
2236 return;
2237 }
2238
2239 if (sm->eapol_cb->eap_param_needed)
2240 sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt);
2241}
2242#else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
2243#define eap_sm_request(sm, type, msg, msglen) do { } while (0)
2244#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
2245
2246const char * eap_sm_get_method_name(struct eap_sm *sm)
2247{
2248 if (sm->m == NULL)
2249 return "UNKNOWN";
2250 return sm->m->name;
2251}
2252
2253
2254/**
2255 * eap_sm_request_identity - Request identity from user (ctrl_iface)
2256 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2257 *
2258 * EAP methods can call this function to request identity information for the
2259 * current network. This is normally called when the identity is not included
2260 * in the network configuration. The request will be sent to monitor programs
2261 * through the control interface.
2262 */
2263void eap_sm_request_identity(struct eap_sm *sm)
2264{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002265 eap_sm_request(sm, WPA_CTRL_REQ_EAP_IDENTITY, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266}
2267
2268
2269/**
2270 * eap_sm_request_password - Request password from user (ctrl_iface)
2271 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2272 *
2273 * EAP methods can call this function to request password information for the
2274 * current network. This is normally called when the password is not included
2275 * in the network configuration. The request will be sent to monitor programs
2276 * through the control interface.
2277 */
2278void eap_sm_request_password(struct eap_sm *sm)
2279{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002280 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSWORD, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281}
2282
2283
2284/**
2285 * eap_sm_request_new_password - Request new password from user (ctrl_iface)
2286 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2287 *
2288 * EAP methods can call this function to request new password information for
2289 * the current network. This is normally called when the EAP method indicates
2290 * that the current password has expired and password change is required. The
2291 * request will be sent to monitor programs through the control interface.
2292 */
2293void eap_sm_request_new_password(struct eap_sm *sm)
2294{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002295 eap_sm_request(sm, WPA_CTRL_REQ_EAP_NEW_PASSWORD, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296}
2297
2298
2299/**
2300 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
2301 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2302 *
2303 * EAP methods can call this function to request SIM or smart card PIN
2304 * information for the current network. This is normally called when the PIN is
2305 * not included in the network configuration. The request will be sent to
2306 * monitor programs through the control interface.
2307 */
2308void eap_sm_request_pin(struct eap_sm *sm)
2309{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002310 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PIN, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311}
2312
2313
2314/**
2315 * eap_sm_request_otp - Request one time password from user (ctrl_iface)
2316 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2317 * @msg: Message to be displayed to the user when asking for OTP
2318 * @msg_len: Length of the user displayable message
2319 *
2320 * EAP methods can call this function to request open time password (OTP) for
2321 * the current network. The request will be sent to monitor programs through
2322 * the control interface.
2323 */
2324void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len)
2325{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002326 eap_sm_request(sm, WPA_CTRL_REQ_EAP_OTP, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327}
2328
2329
2330/**
2331 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
2332 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2333 *
2334 * EAP methods can call this function to request passphrase for a private key
2335 * for the current network. This is normally called when the passphrase is not
2336 * included in the network configuration. The request will be sent to monitor
2337 * programs through the control interface.
2338 */
2339void eap_sm_request_passphrase(struct eap_sm *sm)
2340{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002341 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSPHRASE, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342}
2343
2344
2345/**
Dmitry Shmidt051af732013-10-22 13:52:46 -07002346 * eap_sm_request_sim - Request external SIM processing
2347 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2348 * @req: EAP method specific request
2349 */
2350void eap_sm_request_sim(struct eap_sm *sm, const char *req)
2351{
2352 eap_sm_request(sm, WPA_CTRL_REQ_SIM, req, os_strlen(req));
2353}
2354
2355
2356/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357 * eap_sm_notify_ctrl_attached - Notification of attached monitor
2358 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2359 *
2360 * Notify EAP state machines that a monitor was attached to the control
2361 * interface to trigger re-sending of pending requests for user input.
2362 */
2363void eap_sm_notify_ctrl_attached(struct eap_sm *sm)
2364{
2365 struct eap_peer_config *config = eap_get_config(sm);
2366
2367 if (config == NULL)
2368 return;
2369
2370 /* Re-send any pending requests for user data since a new control
2371 * interface was added. This handles cases where the EAP authentication
2372 * starts immediately after system startup when the user interface is
2373 * not yet running. */
2374 if (config->pending_req_identity)
2375 eap_sm_request_identity(sm);
2376 if (config->pending_req_password)
2377 eap_sm_request_password(sm);
2378 if (config->pending_req_new_password)
2379 eap_sm_request_new_password(sm);
2380 if (config->pending_req_otp)
2381 eap_sm_request_otp(sm, NULL, 0);
2382 if (config->pending_req_pin)
2383 eap_sm_request_pin(sm);
2384 if (config->pending_req_passphrase)
2385 eap_sm_request_passphrase(sm);
2386}
2387
2388
2389static int eap_allowed_phase2_type(int vendor, int type)
2390{
2391 if (vendor != EAP_VENDOR_IETF)
2392 return 0;
2393 return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS &&
2394 type != EAP_TYPE_FAST;
2395}
2396
2397
2398/**
2399 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
2400 * @name: EAP method name, e.g., MD5
2401 * @vendor: Buffer for returning EAP Vendor-Id
2402 * Returns: EAP method type or %EAP_TYPE_NONE if not found
2403 *
2404 * This function maps EAP type names into EAP type numbers that are allowed for
2405 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
2406 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
2407 */
2408u32 eap_get_phase2_type(const char *name, int *vendor)
2409{
2410 int v;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07002411 u32 type = eap_peer_get_type(name, &v);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412 if (eap_allowed_phase2_type(v, type)) {
2413 *vendor = v;
2414 return type;
2415 }
2416 *vendor = EAP_VENDOR_IETF;
2417 return EAP_TYPE_NONE;
2418}
2419
2420
2421/**
2422 * eap_get_phase2_types - Get list of allowed EAP phase 2 types
2423 * @config: Pointer to a network configuration
2424 * @count: Pointer to a variable to be filled with number of returned EAP types
2425 * Returns: Pointer to allocated type list or %NULL on failure
2426 *
2427 * This function generates an array of allowed EAP phase 2 (tunneled) types for
2428 * the given network configuration.
2429 */
2430struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config,
2431 size_t *count)
2432{
2433 struct eap_method_type *buf;
2434 u32 method;
2435 int vendor;
2436 size_t mcount;
2437 const struct eap_method *methods, *m;
2438
2439 methods = eap_peer_get_methods(&mcount);
2440 if (methods == NULL)
2441 return NULL;
2442 *count = 0;
2443 buf = os_malloc(mcount * sizeof(struct eap_method_type));
2444 if (buf == NULL)
2445 return NULL;
2446
2447 for (m = methods; m; m = m->next) {
2448 vendor = m->vendor;
2449 method = m->method;
2450 if (eap_allowed_phase2_type(vendor, method)) {
2451 if (vendor == EAP_VENDOR_IETF &&
2452 method == EAP_TYPE_TLS && config &&
2453 config->private_key2 == NULL)
2454 continue;
2455 buf[*count].vendor = vendor;
2456 buf[*count].method = method;
2457 (*count)++;
2458 }
2459 }
2460
2461 return buf;
2462}
2463
2464
2465/**
2466 * eap_set_fast_reauth - Update fast_reauth setting
2467 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2468 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
2469 */
2470void eap_set_fast_reauth(struct eap_sm *sm, int enabled)
2471{
2472 sm->fast_reauth = enabled;
2473}
2474
2475
2476/**
2477 * eap_set_workaround - Update EAP workarounds setting
2478 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2479 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
2480 */
2481void eap_set_workaround(struct eap_sm *sm, unsigned int workaround)
2482{
2483 sm->workaround = workaround;
2484}
2485
2486
2487/**
2488 * eap_get_config - Get current network configuration
2489 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2490 * Returns: Pointer to the current network configuration or %NULL if not found
2491 *
2492 * EAP peer methods should avoid using this function if they can use other
2493 * access functions, like eap_get_config_identity() and
2494 * eap_get_config_password(), that do not require direct access to
2495 * struct eap_peer_config.
2496 */
2497struct eap_peer_config * eap_get_config(struct eap_sm *sm)
2498{
2499 return sm->eapol_cb->get_config(sm->eapol_ctx);
2500}
2501
2502
2503/**
2504 * eap_get_config_identity - Get identity from the network configuration
2505 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2506 * @len: Buffer for the length of the identity
2507 * Returns: Pointer to the identity or %NULL if not found
2508 */
2509const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
2510{
2511 struct eap_peer_config *config = eap_get_config(sm);
2512 if (config == NULL)
2513 return NULL;
2514 *len = config->identity_len;
2515 return config->identity;
2516}
2517
2518
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002519static int eap_get_ext_password(struct eap_sm *sm,
2520 struct eap_peer_config *config)
2521{
2522 char *name;
2523
2524 if (config->password == NULL)
2525 return -1;
2526
2527 name = os_zalloc(config->password_len + 1);
2528 if (name == NULL)
2529 return -1;
2530 os_memcpy(name, config->password, config->password_len);
2531
2532 ext_password_free(sm->ext_pw_buf);
2533 sm->ext_pw_buf = ext_password_get(sm->ext_pw, name);
2534 os_free(name);
2535
2536 return sm->ext_pw_buf == NULL ? -1 : 0;
2537}
2538
2539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540/**
2541 * eap_get_config_password - Get password from the network configuration
2542 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2543 * @len: Buffer for the length of the password
2544 * Returns: Pointer to the password or %NULL if not found
2545 */
2546const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
2547{
2548 struct eap_peer_config *config = eap_get_config(sm);
2549 if (config == NULL)
2550 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002551
2552 if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
2553 if (eap_get_ext_password(sm, config) < 0)
2554 return NULL;
2555 *len = wpabuf_len(sm->ext_pw_buf);
2556 return wpabuf_head(sm->ext_pw_buf);
2557 }
2558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 *len = config->password_len;
2560 return config->password;
2561}
2562
2563
2564/**
2565 * eap_get_config_password2 - Get password from the network configuration
2566 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2567 * @len: Buffer for the length of the password
2568 * @hash: Buffer for returning whether the password is stored as a
2569 * NtPasswordHash instead of plaintext password; can be %NULL if this
2570 * information is not needed
2571 * Returns: Pointer to the password or %NULL if not found
2572 */
2573const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
2574{
2575 struct eap_peer_config *config = eap_get_config(sm);
2576 if (config == NULL)
2577 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002578
2579 if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
2580 if (eap_get_ext_password(sm, config) < 0)
2581 return NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002582 if (hash)
2583 *hash = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002584 *len = wpabuf_len(sm->ext_pw_buf);
2585 return wpabuf_head(sm->ext_pw_buf);
2586 }
2587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588 *len = config->password_len;
2589 if (hash)
2590 *hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH);
2591 return config->password;
2592}
2593
2594
2595/**
2596 * eap_get_config_new_password - Get new password from network configuration
2597 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2598 * @len: Buffer for the length of the new password
2599 * Returns: Pointer to the new password or %NULL if not found
2600 */
2601const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
2602{
2603 struct eap_peer_config *config = eap_get_config(sm);
2604 if (config == NULL)
2605 return NULL;
2606 *len = config->new_password_len;
2607 return config->new_password;
2608}
2609
2610
2611/**
2612 * eap_get_config_otp - Get one-time password from the network configuration
2613 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2614 * @len: Buffer for the length of the one-time password
2615 * Returns: Pointer to the one-time password or %NULL if not found
2616 */
2617const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len)
2618{
2619 struct eap_peer_config *config = eap_get_config(sm);
2620 if (config == NULL)
2621 return NULL;
2622 *len = config->otp_len;
2623 return config->otp;
2624}
2625
2626
2627/**
2628 * eap_clear_config_otp - Clear used one-time password
2629 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2630 *
2631 * This function clears a used one-time password (OTP) from the current network
2632 * configuration. This should be called when the OTP has been used and is not
2633 * needed anymore.
2634 */
2635void eap_clear_config_otp(struct eap_sm *sm)
2636{
2637 struct eap_peer_config *config = eap_get_config(sm);
2638 if (config == NULL)
2639 return;
2640 os_memset(config->otp, 0, config->otp_len);
2641 os_free(config->otp);
2642 config->otp = NULL;
2643 config->otp_len = 0;
2644}
2645
2646
2647/**
2648 * eap_get_config_phase1 - Get phase1 data from the network configuration
2649 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2650 * Returns: Pointer to the phase1 data or %NULL if not found
2651 */
2652const char * eap_get_config_phase1(struct eap_sm *sm)
2653{
2654 struct eap_peer_config *config = eap_get_config(sm);
2655 if (config == NULL)
2656 return NULL;
2657 return config->phase1;
2658}
2659
2660
2661/**
2662 * eap_get_config_phase2 - Get phase2 data from the network configuration
2663 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2664 * Returns: Pointer to the phase1 data or %NULL if not found
2665 */
2666const char * eap_get_config_phase2(struct eap_sm *sm)
2667{
2668 struct eap_peer_config *config = eap_get_config(sm);
2669 if (config == NULL)
2670 return NULL;
2671 return config->phase2;
2672}
2673
2674
2675int eap_get_config_fragment_size(struct eap_sm *sm)
2676{
2677 struct eap_peer_config *config = eap_get_config(sm);
2678 if (config == NULL)
2679 return -1;
2680 return config->fragment_size;
2681}
2682
2683
2684/**
2685 * eap_key_available - Get key availability (eapKeyAvailable variable)
2686 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2687 * Returns: 1 if EAP keying material is available, 0 if not
2688 */
2689int eap_key_available(struct eap_sm *sm)
2690{
2691 return sm ? sm->eapKeyAvailable : 0;
2692}
2693
2694
2695/**
2696 * eap_notify_success - Notify EAP state machine about external success trigger
2697 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2698 *
2699 * This function is called when external event, e.g., successful completion of
2700 * WPA-PSK key handshake, is indicating that EAP state machine should move to
2701 * success state. This is mainly used with security modes that do not use EAP
2702 * state machine (e.g., WPA-PSK).
2703 */
2704void eap_notify_success(struct eap_sm *sm)
2705{
2706 if (sm) {
2707 sm->decision = DECISION_COND_SUCC;
2708 sm->EAP_state = EAP_SUCCESS;
2709 }
2710}
2711
2712
2713/**
2714 * eap_notify_lower_layer_success - Notification of lower layer success
2715 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2716 *
2717 * Notify EAP state machines that a lower layer has detected a successful
2718 * authentication. This is used to recover from dropped EAP-Success messages.
2719 */
2720void eap_notify_lower_layer_success(struct eap_sm *sm)
2721{
2722 if (sm == NULL)
2723 return;
2724
2725 if (eapol_get_bool(sm, EAPOL_eapSuccess) ||
2726 sm->decision == DECISION_FAIL ||
2727 (sm->methodState != METHOD_MAY_CONT &&
2728 sm->methodState != METHOD_DONE))
2729 return;
2730
2731 if (sm->eapKeyData != NULL)
2732 sm->eapKeyAvailable = TRUE;
2733 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
2734 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
2735 "EAP authentication completed successfully (based on lower "
2736 "layer success)");
2737}
2738
2739
2740/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002741 * eap_get_eapSessionId - Get Session-Id from EAP state machine
2742 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2743 * @len: Pointer to variable that will be set to number of bytes in the session
2744 * Returns: Pointer to the EAP Session-Id or %NULL on failure
2745 *
2746 * Fetch EAP Session-Id from the EAP state machine. The Session-Id is available
2747 * only after a successful authentication. EAP state machine continues to manage
2748 * the Session-Id and the caller must not change or free the returned data.
2749 */
2750const u8 * eap_get_eapSessionId(struct eap_sm *sm, size_t *len)
2751{
2752 if (sm == NULL || sm->eapSessionId == NULL) {
2753 *len = 0;
2754 return NULL;
2755 }
2756
2757 *len = sm->eapSessionIdLen;
2758 return sm->eapSessionId;
2759}
2760
2761
2762/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
2764 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2765 * @len: Pointer to variable that will be set to number of bytes in the key
2766 * Returns: Pointer to the EAP keying data or %NULL on failure
2767 *
2768 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
2769 * key is available only after a successful authentication. EAP state machine
2770 * continues to manage the key data and the caller must not change or free the
2771 * returned data.
2772 */
2773const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len)
2774{
2775 if (sm == NULL || sm->eapKeyData == NULL) {
2776 *len = 0;
2777 return NULL;
2778 }
2779
2780 *len = sm->eapKeyDataLen;
2781 return sm->eapKeyData;
2782}
2783
2784
2785/**
2786 * eap_get_eapKeyData - Get EAP response data
2787 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2788 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
2789 *
2790 * Fetch EAP response (eapRespData) from the EAP state machine. This data is
2791 * available when EAP state machine has processed an incoming EAP request. The
2792 * EAP state machine does not maintain a reference to the response after this
2793 * function is called and the caller is responsible for freeing the data.
2794 */
2795struct wpabuf * eap_get_eapRespData(struct eap_sm *sm)
2796{
2797 struct wpabuf *resp;
2798
2799 if (sm == NULL || sm->eapRespData == NULL)
2800 return NULL;
2801
2802 resp = sm->eapRespData;
2803 sm->eapRespData = NULL;
2804
2805 return resp;
2806}
2807
2808
2809/**
2810 * eap_sm_register_scard_ctx - Notification of smart card context
2811 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2812 * @ctx: Context data for smart card operations
2813 *
2814 * Notify EAP state machines of context data for smart card operations. This
2815 * context data will be used as a parameter for scard_*() functions.
2816 */
2817void eap_register_scard_ctx(struct eap_sm *sm, void *ctx)
2818{
2819 if (sm)
2820 sm->scard_ctx = ctx;
2821}
2822
2823
2824/**
2825 * eap_set_config_blob - Set or add a named configuration blob
2826 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2827 * @blob: New value for the blob
2828 *
2829 * Adds a new configuration blob or replaces the current value of an existing
2830 * blob.
2831 */
2832void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob)
2833{
2834#ifndef CONFIG_NO_CONFIG_BLOBS
2835 sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob);
2836#endif /* CONFIG_NO_CONFIG_BLOBS */
2837}
2838
2839
2840/**
2841 * eap_get_config_blob - Get a named configuration blob
2842 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2843 * @name: Name of the blob
2844 * Returns: Pointer to blob data or %NULL if not found
2845 */
2846const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm,
2847 const char *name)
2848{
2849#ifndef CONFIG_NO_CONFIG_BLOBS
2850 return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name);
2851#else /* CONFIG_NO_CONFIG_BLOBS */
2852 return NULL;
2853#endif /* CONFIG_NO_CONFIG_BLOBS */
2854}
2855
2856
2857/**
2858 * eap_set_force_disabled - Set force_disabled flag
2859 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2860 * @disabled: 1 = EAP disabled, 0 = EAP enabled
2861 *
2862 * This function is used to force EAP state machine to be disabled when it is
2863 * not in use (e.g., with WPA-PSK or plaintext connections).
2864 */
2865void eap_set_force_disabled(struct eap_sm *sm, int disabled)
2866{
2867 sm->force_disabled = disabled;
2868}
2869
2870
Dmitry Shmidt051af732013-10-22 13:52:46 -07002871/**
2872 * eap_set_external_sim - Set external_sim flag
2873 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2874 * @external_sim: Whether external SIM/USIM processing is used
2875 */
2876void eap_set_external_sim(struct eap_sm *sm, int external_sim)
2877{
2878 sm->external_sim = external_sim;
2879}
2880
2881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 /**
2883 * eap_notify_pending - Notify that EAP method is ready to re-process a request
2884 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2885 *
2886 * An EAP method can perform a pending operation (e.g., to get a response from
2887 * an external process). Once the response is available, this function can be
2888 * used to request EAPOL state machine to retry delivering the previously
2889 * received (and still unanswered) EAP request to EAP state machine.
2890 */
2891void eap_notify_pending(struct eap_sm *sm)
2892{
2893 sm->eapol_cb->notify_pending(sm->eapol_ctx);
2894}
2895
2896
2897/**
2898 * eap_invalidate_cached_session - Mark cached session data invalid
2899 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2900 */
2901void eap_invalidate_cached_session(struct eap_sm *sm)
2902{
2903 if (sm)
2904 eap_deinit_prev_method(sm, "invalidate");
2905}
2906
2907
2908int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf)
2909{
2910 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2911 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2912 return 0; /* Not a WPS Enrollee */
2913
2914 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL)
2915 return 0; /* Not using PBC */
2916
2917 return 1;
2918}
2919
2920
2921int eap_is_wps_pin_enrollee(struct eap_peer_config *conf)
2922{
2923 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2924 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2925 return 0; /* Not a WPS Enrollee */
2926
2927 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL)
2928 return 0; /* Not using PIN */
2929
2930 return 1;
2931}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002932
2933
2934void eap_sm_set_ext_pw_ctx(struct eap_sm *sm, struct ext_password_data *ext)
2935{
2936 ext_password_free(sm->ext_pw_buf);
2937 sm->ext_pw_buf = NULL;
2938 sm->ext_pw = ext;
2939}
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002940
2941
2942/**
2943 * eap_set_anon_id - Set or add anonymous identity
2944 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2945 * @id: Anonymous identity (e.g., EAP-SIM pseudonym) or %NULL to clear
2946 * @len: Length of anonymous identity in octets
2947 */
2948void eap_set_anon_id(struct eap_sm *sm, const u8 *id, size_t len)
2949{
2950 if (sm->eapol_cb->set_anon_id)
2951 sm->eapol_cb->set_anon_id(sm->eapol_ctx, id, len);
2952}
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002953
2954
2955int eap_peer_was_failure_expected(struct eap_sm *sm)
2956{
2957 return sm->expected_failure;
2958}