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