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