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