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