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