1 /*
2 * EAP peer state machines (RFC 4137)
3 * Copyright (c) 2004-2019, Jouni Malinen <[email protected]>
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 [email protected]",
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 [email protected]",
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
1775 os_free(sm->identity);
1776 sm->identity = os_memdup(identity, identity_len);
1777 sm->identity_len = identity_len;
1778
1779 wpabuf_free(privacy_identity);
1780
1781 return resp;
1782 }
1783
1784
eap_sm_processNotify(struct eap_sm * sm,const struct wpabuf * req)1785 static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req)
1786 {
1787 const u8 *pos;
1788 char *msg;
1789 size_t i, msg_len;
1790
1791 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, req,
1792 &msg_len);
1793 if (pos == NULL)
1794 return;
1795 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Notification data",
1796 pos, msg_len);
1797
1798 msg = os_malloc(msg_len + 1);
1799 if (msg == NULL)
1800 return;
1801 for (i = 0; i < msg_len; i++)
1802 msg[i] = isprint(pos[i]) ? (char) pos[i] : '_';
1803 msg[msg_len] = '\0';
1804 wpa_msg(sm->msg_ctx, MSG_INFO, "%s%s",
1805 WPA_EVENT_EAP_NOTIFICATION, msg);
1806 os_free(msg);
1807 }
1808
1809
eap_sm_buildNotify(int id)1810 static struct wpabuf * eap_sm_buildNotify(int id)
1811 {
1812 wpa_printf(MSG_DEBUG, "EAP: Generating EAP-Response Notification");
1813 return eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, 0,
1814 EAP_CODE_RESPONSE, id);
1815 }
1816
1817
eap_peer_initiate(struct eap_sm * sm,const struct eap_hdr * hdr,size_t len)1818 static void eap_peer_initiate(struct eap_sm *sm, const struct eap_hdr *hdr,
1819 size_t len)
1820 {
1821 #ifdef CONFIG_ERP
1822 const u8 *pos = (const u8 *) (hdr + 1);
1823 const u8 *end = ((const u8 *) hdr) + len;
1824 struct erp_tlvs parse;
1825
1826 if (len < sizeof(*hdr) + 1) {
1827 wpa_printf(MSG_DEBUG, "EAP: Ignored too short EAP-Initiate");
1828 return;
1829 }
1830
1831 if (*pos != EAP_ERP_TYPE_REAUTH_START) {
1832 wpa_printf(MSG_DEBUG,
1833 "EAP: Ignored unexpected EAP-Initiate Type=%u",
1834 *pos);
1835 return;
1836 }
1837
1838 pos++;
1839 if (pos >= end) {
1840 wpa_printf(MSG_DEBUG,
1841 "EAP: Too short EAP-Initiate/Re-auth-Start");
1842 return;
1843 }
1844 pos++; /* Reserved */
1845 wpa_hexdump(MSG_DEBUG, "EAP: EAP-Initiate/Re-auth-Start TVs/TLVs",
1846 pos, end - pos);
1847
1848 if (erp_parse_tlvs(pos, end, &parse, 0) < 0)
1849 goto invalid;
1850
1851 if (parse.domain) {
1852 wpa_hexdump_ascii(MSG_DEBUG,
1853 "EAP: EAP-Initiate/Re-auth-Start - Domain name",
1854 parse.domain, parse.domain_len);
1855 /* TODO: Derivation of domain specific keys for local ER */
1856 }
1857
1858 if (eap_peer_erp_reauth_start(sm, hdr->identifier) == 0)
1859 return;
1860
1861 invalid:
1862 #endif /* CONFIG_ERP */
1863 wpa_printf(MSG_DEBUG,
1864 "EAP: EAP-Initiate/Re-auth-Start - No suitable ERP keys available - try to start full EAP authentication");
1865 eapol_set_bool(sm, EAPOL_eapTriggerStart, true);
1866 }
1867
1868
eap_peer_finish(struct eap_sm * sm,const struct eap_hdr * hdr,size_t len)1869 void eap_peer_finish(struct eap_sm *sm, const struct eap_hdr *hdr, size_t len)
1870 {
1871 #ifdef CONFIG_ERP
1872 const u8 *pos = (const u8 *) (hdr + 1);
1873 const u8 *end = ((const u8 *) hdr) + len;
1874 const u8 *start;
1875 struct erp_tlvs parse;
1876 u8 flags;
1877 u16 seq;
1878 u8 hash[SHA256_MAC_LEN];
1879 size_t hash_len;
1880 struct eap_erp_key *erp;
1881 int max_len;
1882 char nai[254];
1883 u8 seed[4];
1884 int auth_tag_ok = 0;
1885
1886 if (len < sizeof(*hdr) + 1) {
1887 wpa_printf(MSG_DEBUG, "EAP: Ignored too short EAP-Finish");
1888 return;
1889 }
1890
1891 if (*pos != EAP_ERP_TYPE_REAUTH) {
1892 wpa_printf(MSG_DEBUG,
1893 "EAP: Ignored unexpected EAP-Finish Type=%u", *pos);
1894 return;
1895 }
1896
1897 if (len < sizeof(*hdr) + 4) {
1898 wpa_printf(MSG_DEBUG,
1899 "EAP: Ignored too short EAP-Finish/Re-auth");
1900 return;
1901 }
1902
1903 pos++;
1904 flags = *pos++;
1905 seq = WPA_GET_BE16(pos);
1906 pos += 2;
1907 wpa_printf(MSG_DEBUG, "EAP: Flags=0x%x SEQ=%u", flags, seq);
1908
1909 if (seq != sm->erp_seq) {
1910 wpa_printf(MSG_DEBUG,
1911 "EAP: Unexpected EAP-Finish/Re-auth SEQ=%u", seq);
1912 return;
1913 }
1914
1915 /*
1916 * Parse TVs/TLVs. Since we do not yet know the length of the
1917 * Authentication Tag, stop parsing if an unknown TV/TLV is seen and
1918 * just try to find the keyName-NAI first so that we can check the
1919 * Authentication Tag.
1920 */
1921 if (erp_parse_tlvs(pos, end, &parse, 1) < 0)
1922 return;
1923
1924 if (!parse.keyname) {
1925 wpa_printf(MSG_DEBUG,
1926 "EAP: No keyName-NAI in EAP-Finish/Re-auth Packet");
1927 return;
1928 }
1929
1930 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Finish/Re-auth - keyName-NAI",
1931 parse.keyname, parse.keyname_len);
1932 if (parse.keyname_len > 253) {
1933 wpa_printf(MSG_DEBUG,
1934 "EAP: Too long keyName-NAI in EAP-Finish/Re-auth");
1935 return;
1936 }
1937 os_memcpy(nai, parse.keyname, parse.keyname_len);
1938 nai[parse.keyname_len] = '\0';
1939
1940 erp = eap_erp_get_key_nai(sm, nai);
1941 if (!erp) {
1942 wpa_printf(MSG_DEBUG, "EAP: No matching ERP key found for %s",
1943 nai);
1944 return;
1945 }
1946
1947 /* Is there enough room for Cryptosuite and Authentication Tag? */
1948 start = parse.keyname + parse.keyname_len;
1949 max_len = end - start;
1950 hash_len = 16;
1951 if (max_len < 1 + (int) hash_len) {
1952 wpa_printf(MSG_DEBUG,
1953 "EAP: Not enough room for Authentication Tag");
1954 if (flags & 0x80)
1955 goto no_auth_tag;
1956 return;
1957 }
1958 if (end[-17] != EAP_ERP_CS_HMAC_SHA256_128) {
1959 wpa_printf(MSG_DEBUG, "EAP: Different Cryptosuite used");
1960 if (flags & 0x80)
1961 goto no_auth_tag;
1962 return;
1963 }
1964
1965 if (hmac_sha256(erp->rIK, erp->rIK_len, (const u8 *) hdr,
1966 end - ((const u8 *) hdr) - hash_len, hash) < 0)
1967 return;
1968 if (os_memcmp(end - hash_len, hash, hash_len) != 0) {
1969 wpa_printf(MSG_DEBUG,
1970 "EAP: Authentication Tag mismatch");
1971 return;
1972 }
1973 auth_tag_ok = 1;
1974 end -= 1 + hash_len;
1975
1976 no_auth_tag:
1977 /*
1978 * Parse TVs/TLVs again now that we know the exact part of the buffer
1979 * that contains them.
1980 */
1981 wpa_hexdump(MSG_DEBUG, "EAP: EAP-Finish/Re-Auth TVs/TLVs",
1982 pos, end - pos);
1983 if (erp_parse_tlvs(pos, end, &parse, 0) < 0)
1984 return;
1985
1986 if (flags & 0x80 || !auth_tag_ok) {
1987 wpa_printf(MSG_DEBUG,
1988 "EAP: EAP-Finish/Re-auth indicated failure");
1989 eapol_set_bool(sm, EAPOL_eapFail, true);
1990 eapol_set_bool(sm, EAPOL_eapReq, false);
1991 eapol_set_bool(sm, EAPOL_eapNoResp, true);
1992 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
1993 "EAP authentication failed");
1994 sm->prev_failure = 1;
1995 wpa_printf(MSG_DEBUG,
1996 "EAP: Drop ERP key to try full authentication on next attempt");
1997 eap_peer_erp_free_key(erp);
1998 return;
1999 }
2000
2001 eap_sm_free_key(sm);
2002 sm->eapKeyDataLen = 0;
2003 sm->eapKeyData = os_malloc(erp->rRK_len);
2004 if (!sm->eapKeyData)
2005 return;
2006 sm->eapKeyDataLen = erp->rRK_len;
2007
2008 WPA_PUT_BE16(seed, seq);
2009 WPA_PUT_BE16(&seed[2], erp->rRK_len);
2010 if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
2011 "Re-authentication Master Session [email protected]",
2012 seed, sizeof(seed),
2013 sm->eapKeyData, erp->rRK_len) < 0) {
2014 wpa_printf(MSG_DEBUG, "EAP: Could not derive rMSK for ERP");
2015 eap_sm_free_key(sm);
2016 return;
2017 }
2018 wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rMSK",
2019 sm->eapKeyData, sm->eapKeyDataLen);
2020 sm->eapKeyAvailable = true;
2021 eapol_set_bool(sm, EAPOL_eapSuccess, true);
2022 eapol_set_bool(sm, EAPOL_eapReq, false);
2023 eapol_set_bool(sm, EAPOL_eapNoResp, true);
2024 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
2025 "EAP re-authentication completed successfully");
2026 #endif /* CONFIG_ERP */
2027 }
2028
2029
eap_sm_parseEapReq(struct eap_sm * sm,const struct wpabuf * req)2030 static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req)
2031 {
2032 const struct eap_hdr *hdr;
2033 size_t plen;
2034 const u8 *pos;
2035
2036 sm->rxReq = sm->rxResp = sm->rxSuccess = sm->rxFailure = false;
2037 sm->reqId = 0;
2038 sm->reqMethod = EAP_TYPE_NONE;
2039 sm->reqVendor = EAP_VENDOR_IETF;
2040 sm->reqVendorMethod = EAP_TYPE_NONE;
2041
2042 if (req == NULL || wpabuf_len(req) < sizeof(*hdr))
2043 return;
2044
2045 hdr = wpabuf_head(req);
2046 plen = be_to_host16(hdr->length);
2047 if (plen > wpabuf_len(req)) {
2048 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
2049 "(len=%lu plen=%lu)",
2050 (unsigned long) wpabuf_len(req),
2051 (unsigned long) plen);
2052 return;
2053 }
2054
2055 sm->reqId = hdr->identifier;
2056
2057 if (sm->workaround) {
2058 const u8 *addr[1];
2059 addr[0] = wpabuf_head(req);
2060 sha1_vector(1, addr, &plen, sm->req_sha1);
2061 }
2062
2063 switch (hdr->code) {
2064 case EAP_CODE_REQUEST:
2065 if (plen < sizeof(*hdr) + 1) {
2066 wpa_printf(MSG_DEBUG, "EAP: Too short EAP-Request - "
2067 "no Type field");
2068 return;
2069 }
2070 sm->rxReq = true;
2071 pos = (const u8 *) (hdr + 1);
2072 sm->reqMethod = *pos++;
2073 if (sm->reqMethod == EAP_TYPE_EXPANDED) {
2074 if (plen < sizeof(*hdr) + 8) {
2075 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
2076 "expanded EAP-Packet (plen=%lu)",
2077 (unsigned long) plen);
2078 return;
2079 }
2080 sm->reqVendor = WPA_GET_BE24(pos);
2081 pos += 3;
2082 sm->reqVendorMethod = WPA_GET_BE32(pos);
2083 }
2084 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Request id=%d "
2085 "method=%u vendor=%u vendorMethod=%u",
2086 sm->reqId, sm->reqMethod, sm->reqVendor,
2087 sm->reqVendorMethod);
2088 break;
2089 case EAP_CODE_RESPONSE:
2090 if (sm->selectedMethod == EAP_TYPE_LEAP) {
2091 /*
2092 * LEAP differs from RFC 4137 by using reversed roles
2093 * for mutual authentication and because of this, we
2094 * need to accept EAP-Response frames if LEAP is used.
2095 */
2096 if (plen < sizeof(*hdr) + 1) {
2097 wpa_printf(MSG_DEBUG, "EAP: Too short "
2098 "EAP-Response - no Type field");
2099 return;
2100 }
2101 sm->rxResp = true;
2102 pos = (const u8 *) (hdr + 1);
2103 sm->reqMethod = *pos;
2104 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Response for "
2105 "LEAP method=%d id=%d",
2106 sm->reqMethod, sm->reqId);
2107 break;
2108 }
2109 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Response");
2110 break;
2111 case EAP_CODE_SUCCESS:
2112 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Success");
2113 eap_notify_status(sm, "completion", "success");
2114 sm->rxSuccess = true;
2115 break;
2116 case EAP_CODE_FAILURE:
2117 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Failure");
2118 eap_notify_status(sm, "completion", "failure");
2119
2120 /* Get the error code from method */
2121 if (sm->m && sm->m->get_error_code) {
2122 int error_code;
2123
2124 error_code = sm->m->get_error_code(sm->eap_method_priv);
2125 if (error_code != NO_EAP_METHOD_ERROR)
2126 eap_report_error(sm, error_code);
2127 }
2128 sm->rxFailure = true;
2129 break;
2130 case EAP_CODE_INITIATE:
2131 eap_peer_initiate(sm, hdr, plen);
2132 break;
2133 case EAP_CODE_FINISH:
2134 eap_peer_finish(sm, hdr, plen);
2135 break;
2136 default:
2137 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Packet with unknown "
2138 "code %d", hdr->code);
2139 break;
2140 }
2141 }
2142
2143
eap_peer_sm_tls_event(void * ctx,enum tls_event ev,union tls_event_data * data)2144 static void eap_peer_sm_tls_event(void *ctx, enum tls_event ev,
2145 union tls_event_data *data)
2146 {
2147 struct eap_sm *sm = ctx;
2148 char *hash_hex = NULL;
2149
2150 switch (ev) {
2151 case TLS_CERT_CHAIN_SUCCESS:
2152 eap_notify_status(sm, "remote certificate verification",
2153 "success");
2154 if (sm->ext_cert_check) {
2155 sm->waiting_ext_cert_check = 1;
2156 eap_sm_request(sm, WPA_CTRL_REQ_EXT_CERT_CHECK,
2157 NULL, 0);
2158 }
2159 break;
2160 case TLS_CERT_CHAIN_FAILURE:
2161 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_TLS_CERT_ERROR
2162 "reason=%d depth=%d subject='%s' err='%s'",
2163 data->cert_fail.reason,
2164 data->cert_fail.depth,
2165 data->cert_fail.subject,
2166 data->cert_fail.reason_txt);
2167 eap_notify_status(sm, "remote certificate verification",
2168 data->cert_fail.reason_txt);
2169 break;
2170 case TLS_PEER_CERTIFICATE:
2171 if (!sm->eapol_cb->notify_cert)
2172 break;
2173
2174 if (data->peer_cert.hash) {
2175 size_t len = data->peer_cert.hash_len * 2 + 1;
2176 hash_hex = os_malloc(len);
2177 if (hash_hex) {
2178 wpa_snprintf_hex(hash_hex, len,
2179 data->peer_cert.hash,
2180 data->peer_cert.hash_len);
2181 }
2182 }
2183
2184 sm->eapol_cb->notify_cert(sm->eapol_ctx, &data->peer_cert,
2185 hash_hex);
2186 break;
2187 case TLS_ALERT:
2188 if (data->alert.is_local)
2189 eap_notify_status(sm, "local TLS alert",
2190 data->alert.description);
2191 else
2192 eap_notify_status(sm, "remote TLS alert",
2193 data->alert.description);
2194 break;
2195 case TLS_UNSAFE_RENEGOTIATION_DISABLED:
2196 wpa_printf(MSG_INFO,
2197 "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");
2198 eap_notify_status(sm, "unsafe server renegotiation", "failure");
2199 break;
2200 }
2201
2202 os_free(hash_hex);
2203 }
2204
tls_certificate_callback(void * ctx,const char * alias,uint8_t ** value)2205 ssize_t tls_certificate_callback(void* ctx, const char* alias, uint8_t** value) {
2206 if (alias == NULL || ctx == NULL || value == NULL) return -1;
2207 struct eap_sm *sm = (struct eap_sm*) ctx;
2208 wpa_printf(MSG_INFO, "tls_certificate_callback: received sm=%p", (void*)sm);
2209 if (sm->eapol_cb && sm->eapol_cb->get_certificate) {
2210 return sm->eapol_cb->get_certificate(sm->eapol_ctx, alias, value);
2211 }
2212 return -1;
2213 }
2214
tls_openssl_failure_callback(void * ctx,const char * msg)2215 void tls_openssl_failure_callback(void* ctx, const char* msg) {
2216 if (ctx == NULL || msg == NULL) return;
2217 struct eap_sm *sm = (struct eap_sm*) ctx;
2218 if (sm->eapol_cb && sm->eapol_cb->notify_open_ssl_failure) {
2219 sm->eapol_cb->notify_open_ssl_failure(sm->eapol_ctx, msg);
2220 }
2221 }
2222
2223 /**
2224 * eap_peer_sm_init - Allocate and initialize EAP peer state machine
2225 * @eapol_ctx: Context data to be used with eapol_cb calls
2226 * @eapol_cb: Pointer to EAPOL callback functions
2227 * @msg_ctx: Context data for wpa_msg() calls
2228 * @conf: EAP configuration
2229 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
2230 *
2231 * This function allocates and initializes an EAP state machine. In addition,
2232 * this initializes TLS library for the new EAP state machine. eapol_cb pointer
2233 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
2234 * state machine. Consequently, the caller must make sure that this data
2235 * structure remains alive while the EAP state machine is active.
2236 */
eap_peer_sm_init(void * eapol_ctx,const struct eapol_callbacks * eapol_cb,void * msg_ctx,struct eap_config * conf)2237 struct eap_sm * eap_peer_sm_init(void *eapol_ctx,
2238 const struct eapol_callbacks *eapol_cb,
2239 void *msg_ctx, struct eap_config *conf)
2240 {
2241 struct eap_sm *sm;
2242 struct tls_config tlsconf;
2243
2244 sm = os_zalloc(sizeof(*sm));
2245 wpa_printf(MSG_INFO, "Init sm=%p", (void*)sm);
2246 if (sm == NULL)
2247 return NULL;
2248 sm->eapol_ctx = eapol_ctx;
2249 sm->eapol_cb = eapol_cb;
2250 sm->msg_ctx = msg_ctx;
2251 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
2252 sm->wps = conf->wps;
2253 dl_list_init(&sm->erp_keys);
2254
2255 os_memset(&tlsconf, 0, sizeof(tlsconf));
2256 #ifndef CONFIG_OPENSC_ENGINE_PATH
2257 tlsconf.opensc_engine_path = conf->opensc_engine_path;
2258 #endif /* CONFIG_OPENSC_ENGINE_PATH */
2259 #ifndef CONFIG_PKCS11_ENGINE_PATH
2260 tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path;
2261 #endif /* CONFIG_PKCS11_ENGINE_PATH */
2262 #ifndef CONFIG_PKCS11_MODULE_PATH
2263 tlsconf.pkcs11_module_path = conf->pkcs11_module_path;
2264 #endif /* CONFIG_PKCS11_MODULE_PATH */
2265 tlsconf.openssl_ciphers = conf->openssl_ciphers;
2266 #ifdef CONFIG_FIPS
2267 tlsconf.fips_mode = 1;
2268 #endif /* CONFIG_FIPS */
2269 tlsconf.event_cb = eap_peer_sm_tls_event;
2270 tlsconf.cb_ctx = sm;
2271 tlsconf.cert_in_cb = conf->cert_in_cb;
2272 tls_register_cert_callback(&tls_certificate_callback);
2273 tls_register_openssl_failure_callback(&tls_openssl_failure_callback);
2274 sm->ssl_ctx = tls_init(&tlsconf);
2275 if (sm->ssl_ctx == NULL) {
2276 wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS "
2277 "context.");
2278 os_free(sm);
2279 return NULL;
2280 }
2281
2282 sm->ssl_ctx2 = tls_init(&tlsconf);
2283 if (sm->ssl_ctx2 == NULL) {
2284 wpa_printf(MSG_INFO, "SSL: Failed to initialize TLS "
2285 "context (2).");
2286 /* Run without separate TLS context within TLS tunnel */
2287 }
2288
2289 return sm;
2290 }
2291
2292
2293 /**
2294 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
2295 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2296 *
2297 * This function deinitializes EAP state machine and frees all allocated
2298 * resources.
2299 */
eap_peer_sm_deinit(struct eap_sm * sm)2300 void eap_peer_sm_deinit(struct eap_sm *sm)
2301 {
2302 wpa_printf(MSG_INFO, "Deinit sm=%p", (void*)sm);
2303 if (sm == NULL)
2304 return;
2305 eap_deinit_prev_method(sm, "EAP deinit");
2306 eap_sm_abort(sm);
2307 if (sm->ssl_ctx2)
2308 tls_deinit(sm->ssl_ctx2);
2309 tls_deinit(sm->ssl_ctx);
2310 eap_peer_erp_free_keys(sm);
2311 os_free(sm->identity);
2312 os_free(sm);
2313 }
2314
2315
2316 /**
2317 * eap_peer_sm_step - Step EAP peer state machine
2318 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2319 * Returns: 1 if EAP state was changed or 0 if not
2320 *
2321 * This function advances EAP state machine to a new state to match with the
2322 * current variables. This should be called whenever variables used by the EAP
2323 * state machine have changed.
2324 */
eap_peer_sm_step(struct eap_sm * sm)2325 int eap_peer_sm_step(struct eap_sm *sm)
2326 {
2327 int res = 0;
2328 do {
2329 sm->changed = false;
2330 SM_STEP_RUN(EAP);
2331 if (sm->changed)
2332 res = 1;
2333 } while (sm->changed);
2334 return res;
2335 }
2336
2337
2338 /**
2339 * eap_sm_abort - Abort EAP authentication
2340 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2341 *
2342 * Release system resources that have been allocated for the authentication
2343 * session without fully deinitializing the EAP state machine.
2344 */
eap_sm_abort(struct eap_sm * sm)2345 void eap_sm_abort(struct eap_sm *sm)
2346 {
2347 wpabuf_free(sm->lastRespData);
2348 sm->lastRespData = NULL;
2349 wpabuf_free(sm->eapRespData);
2350 sm->eapRespData = NULL;
2351 eap_sm_free_key(sm);
2352 os_free(sm->eapSessionId);
2353 sm->eapSessionId = NULL;
2354
2355 /* This is not clearly specified in the EAP statemachines draft, but
2356 * it seems necessary to make sure that some of the EAPOL variables get
2357 * cleared for the next authentication. */
2358 eapol_set_bool(sm, EAPOL_eapSuccess, false);
2359 }
2360
2361
2362 #ifdef CONFIG_CTRL_IFACE
eap_sm_state_txt(int state)2363 static const char * eap_sm_state_txt(int state)
2364 {
2365 switch (state) {
2366 case EAP_INITIALIZE:
2367 return "INITIALIZE";
2368 case EAP_DISABLED:
2369 return "DISABLED";
2370 case EAP_IDLE:
2371 return "IDLE";
2372 case EAP_RECEIVED:
2373 return "RECEIVED";
2374 case EAP_GET_METHOD:
2375 return "GET_METHOD";
2376 case EAP_METHOD:
2377 return "METHOD";
2378 case EAP_SEND_RESPONSE:
2379 return "SEND_RESPONSE";
2380 case EAP_DISCARD:
2381 return "DISCARD";
2382 case EAP_IDENTITY:
2383 return "IDENTITY";
2384 case EAP_NOTIFICATION:
2385 return "NOTIFICATION";
2386 case EAP_RETRANSMIT:
2387 return "RETRANSMIT";
2388 case EAP_SUCCESS:
2389 return "SUCCESS";
2390 case EAP_FAILURE:
2391 return "FAILURE";
2392 default:
2393 return "UNKNOWN";
2394 }
2395 }
2396 #endif /* CONFIG_CTRL_IFACE */
2397
2398
2399 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
eap_sm_method_state_txt(EapMethodState state)2400 static const char * eap_sm_method_state_txt(EapMethodState state)
2401 {
2402 switch (state) {
2403 case METHOD_NONE:
2404 return "NONE";
2405 case METHOD_INIT:
2406 return "INIT";
2407 case METHOD_CONT:
2408 return "CONT";
2409 case METHOD_MAY_CONT:
2410 return "MAY_CONT";
2411 case METHOD_DONE:
2412 return "DONE";
2413 default:
2414 return "UNKNOWN";
2415 }
2416 }
2417
2418
eap_sm_decision_txt(EapDecision decision)2419 static const char * eap_sm_decision_txt(EapDecision decision)
2420 {
2421 switch (decision) {
2422 case DECISION_FAIL:
2423 return "FAIL";
2424 case DECISION_COND_SUCC:
2425 return "COND_SUCC";
2426 case DECISION_UNCOND_SUCC:
2427 return "UNCOND_SUCC";
2428 default:
2429 return "UNKNOWN";
2430 }
2431 }
2432 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
2433
2434
2435 #ifdef CONFIG_CTRL_IFACE
2436
2437 /**
2438 * eap_sm_get_status - Get EAP state machine status
2439 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2440 * @buf: Buffer for status information
2441 * @buflen: Maximum buffer length
2442 * @verbose: Whether to include verbose status information
2443 * Returns: Number of bytes written to buf.
2444 *
2445 * Query EAP state machine for status information. This function fills in a
2446 * text area with current status information from the EAPOL state machine. If
2447 * the buffer (buf) is not large enough, status information will be truncated
2448 * to fit the buffer.
2449 */
eap_sm_get_status(struct eap_sm * sm,char * buf,size_t buflen,int verbose)2450 int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose)
2451 {
2452 int len, ret;
2453
2454 if (sm == NULL)
2455 return 0;
2456
2457 len = os_snprintf(buf, buflen,
2458 "EAP state=%s\n",
2459 eap_sm_state_txt(sm->EAP_state));
2460 if (os_snprintf_error(buflen, len))
2461 return 0;
2462
2463 if (sm->selectedMethod != EAP_TYPE_NONE) {
2464 const char *name;
2465 if (sm->m) {
2466 name = sm->m->name;
2467 } else {
2468 const struct eap_method *m =
2469 eap_peer_get_eap_method(EAP_VENDOR_IETF,
2470 sm->selectedMethod);
2471 if (m)
2472 name = m->name;
2473 else
2474 name = "?";
2475 }
2476 ret = os_snprintf(buf + len, buflen - len,
2477 "selectedMethod=%d (EAP-%s)\n",
2478 sm->selectedMethod, name);
2479 if (os_snprintf_error(buflen - len, ret))
2480 return len;
2481 len += ret;
2482
2483 if (sm->m && sm->m->get_status) {
2484 len += sm->m->get_status(sm, sm->eap_method_priv,
2485 buf + len, buflen - len,
2486 verbose);
2487 }
2488 }
2489
2490 if (verbose) {
2491 ret = os_snprintf(buf + len, buflen - len,
2492 "reqMethod=%d\n"
2493 "methodState=%s\n"
2494 "decision=%s\n"
2495 "ClientTimeout=%d\n",
2496 sm->reqMethod,
2497 eap_sm_method_state_txt(sm->methodState),
2498 eap_sm_decision_txt(sm->decision),
2499 sm->ClientTimeout);
2500 if (os_snprintf_error(buflen - len, ret))
2501 return len;
2502 len += ret;
2503 }
2504
2505 return len;
2506 }
2507 #endif /* CONFIG_CTRL_IFACE */
2508
2509
eap_sm_request(struct eap_sm * sm,enum wpa_ctrl_req_type field,const char * msg,size_t msglen)2510 static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field,
2511 const char *msg, size_t msglen)
2512 {
2513 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
2514 struct eap_peer_config *config;
2515 const char *txt = NULL;
2516 char *tmp;
2517
2518 if (sm == NULL)
2519 return;
2520 config = eap_get_config(sm);
2521 if (config == NULL)
2522 return;
2523
2524 switch (field) {
2525 case WPA_CTRL_REQ_EAP_IDENTITY:
2526 config->pending_req_identity++;
2527 break;
2528 case WPA_CTRL_REQ_EAP_PASSWORD:
2529 config->pending_req_password++;
2530 break;
2531 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
2532 config->pending_req_new_password++;
2533 break;
2534 case WPA_CTRL_REQ_EAP_PIN:
2535 config->pending_req_pin++;
2536 break;
2537 case WPA_CTRL_REQ_EAP_OTP:
2538 if (msg) {
2539 tmp = os_malloc(msglen + 3);
2540 if (tmp == NULL)
2541 return;
2542 tmp[0] = '[';
2543 os_memcpy(tmp + 1, msg, msglen);
2544 tmp[msglen + 1] = ']';
2545 tmp[msglen + 2] = '\0';
2546 txt = tmp;
2547 os_free(config->pending_req_otp);
2548 config->pending_req_otp = tmp;
2549 config->pending_req_otp_len = msglen + 3;
2550 } else {
2551 if (config->pending_req_otp == NULL)
2552 return;
2553 txt = config->pending_req_otp;
2554 }
2555 break;
2556 case WPA_CTRL_REQ_EAP_PASSPHRASE:
2557 config->pending_req_passphrase++;
2558 break;
2559 case WPA_CTRL_REQ_SIM:
2560 config->pending_req_sim++;
2561 txt = msg;
2562 break;
2563 case WPA_CTRL_REQ_EXT_CERT_CHECK:
2564 break;
2565 default:
2566 return;
2567 }
2568
2569 if (sm->eapol_cb->eap_param_needed)
2570 sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt);
2571 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
2572 }
2573
2574
eap_sm_get_method_name(struct eap_sm * sm)2575 const char * eap_sm_get_method_name(struct eap_sm *sm)
2576 {
2577 if (sm->m == NULL)
2578 return "UNKNOWN";
2579 return sm->m->name;
2580 }
2581
2582
2583 /**
2584 * eap_sm_request_identity - Request identity from user (ctrl_iface)
2585 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2586 *
2587 * EAP methods can call this function to request identity information for the
2588 * current network. This is normally called when the identity is not included
2589 * in the network configuration. The request will be sent to monitor programs
2590 * through the control interface.
2591 */
eap_sm_request_identity(struct eap_sm * sm)2592 void eap_sm_request_identity(struct eap_sm *sm)
2593 {
2594 eap_sm_request(sm, WPA_CTRL_REQ_EAP_IDENTITY, NULL, 0);
2595 }
2596
2597
2598 /**
2599 * eap_sm_request_password - Request password from user (ctrl_iface)
2600 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2601 *
2602 * EAP methods can call this function to request password information for the
2603 * current network. This is normally called when the password is not included
2604 * in the network configuration. The request will be sent to monitor programs
2605 * through the control interface.
2606 */
eap_sm_request_password(struct eap_sm * sm)2607 void eap_sm_request_password(struct eap_sm *sm)
2608 {
2609 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSWORD, NULL, 0);
2610 }
2611
2612
2613 /**
2614 * eap_sm_request_new_password - Request new password from user (ctrl_iface)
2615 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2616 *
2617 * EAP methods can call this function to request new password information for
2618 * the current network. This is normally called when the EAP method indicates
2619 * that the current password has expired and password change is required. The
2620 * request will be sent to monitor programs through the control interface.
2621 */
eap_sm_request_new_password(struct eap_sm * sm)2622 void eap_sm_request_new_password(struct eap_sm *sm)
2623 {
2624 eap_sm_request(sm, WPA_CTRL_REQ_EAP_NEW_PASSWORD, NULL, 0);
2625 }
2626
2627
2628 /**
2629 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
2630 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2631 *
2632 * EAP methods can call this function to request SIM or smart card PIN
2633 * information for the current network. This is normally called when the PIN is
2634 * not included in the network configuration. The request will be sent to
2635 * monitor programs through the control interface.
2636 */
eap_sm_request_pin(struct eap_sm * sm)2637 void eap_sm_request_pin(struct eap_sm *sm)
2638 {
2639 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PIN, NULL, 0);
2640 }
2641
2642
2643 /**
2644 * eap_sm_request_otp - Request one time password from user (ctrl_iface)
2645 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2646 * @msg: Message to be displayed to the user when asking for OTP
2647 * @msg_len: Length of the user displayable message
2648 *
2649 * EAP methods can call this function to request open time password (OTP) for
2650 * the current network. The request will be sent to monitor programs through
2651 * the control interface.
2652 */
eap_sm_request_otp(struct eap_sm * sm,const char * msg,size_t msg_len)2653 void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len)
2654 {
2655 eap_sm_request(sm, WPA_CTRL_REQ_EAP_OTP, msg, msg_len);
2656 }
2657
2658
2659 /**
2660 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
2661 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2662 *
2663 * EAP methods can call this function to request passphrase for a private key
2664 * for the current network. This is normally called when the passphrase is not
2665 * included in the network configuration. The request will be sent to monitor
2666 * programs through the control interface.
2667 */
eap_sm_request_passphrase(struct eap_sm * sm)2668 void eap_sm_request_passphrase(struct eap_sm *sm)
2669 {
2670 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSPHRASE, NULL, 0);
2671 }
2672
2673
2674 /**
2675 * eap_sm_request_sim - Request external SIM processing
2676 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2677 * @req: EAP method specific request
2678 */
eap_sm_request_sim(struct eap_sm * sm,const char * req)2679 void eap_sm_request_sim(struct eap_sm *sm, const char *req)
2680 {
2681 eap_sm_request(sm, WPA_CTRL_REQ_SIM, req, os_strlen(req));
2682 }
2683
2684
2685 /**
2686 * eap_sm_notify_ctrl_attached - Notification of attached monitor
2687 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2688 *
2689 * Notify EAP state machines that a monitor was attached to the control
2690 * interface to trigger re-sending of pending requests for user input.
2691 */
eap_sm_notify_ctrl_attached(struct eap_sm * sm)2692 void eap_sm_notify_ctrl_attached(struct eap_sm *sm)
2693 {
2694 struct eap_peer_config *config = eap_get_config(sm);
2695
2696 if (config == NULL)
2697 return;
2698
2699 /* Re-send any pending requests for user data since a new control
2700 * interface was added. This handles cases where the EAP authentication
2701 * starts immediately after system startup when the user interface is
2702 * not yet running. */
2703 if (config->pending_req_identity)
2704 eap_sm_request_identity(sm);
2705 if (config->pending_req_password)
2706 eap_sm_request_password(sm);
2707 if (config->pending_req_new_password)
2708 eap_sm_request_new_password(sm);
2709 if (config->pending_req_otp)
2710 eap_sm_request_otp(sm, NULL, 0);
2711 if (config->pending_req_pin)
2712 eap_sm_request_pin(sm);
2713 if (config->pending_req_passphrase)
2714 eap_sm_request_passphrase(sm);
2715 }
2716
2717
eap_allowed_phase2_type(int vendor,int type)2718 static int eap_allowed_phase2_type(int vendor, int type)
2719 {
2720 if (vendor == EAP_VENDOR_HOSTAP)
2721 return 1;
2722 if (vendor != EAP_VENDOR_IETF)
2723 return 0;
2724 return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS &&
2725 type != EAP_TYPE_FAST && type != EAP_TYPE_TEAP;
2726 }
2727
2728
2729 /**
2730 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
2731 * @name: EAP method name, e.g., MD5
2732 * @vendor: Buffer for returning EAP Vendor-Id
2733 * Returns: EAP method type or %EAP_TYPE_NONE if not found
2734 *
2735 * This function maps EAP type names into EAP type numbers that are allowed for
2736 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
2737 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
2738 */
eap_get_phase2_type(const char * name,int * vendor)2739 u32 eap_get_phase2_type(const char *name, int *vendor)
2740 {
2741 int v;
2742 u32 type = eap_peer_get_type(name, &v);
2743 if (eap_allowed_phase2_type(v, type)) {
2744 *vendor = v;
2745 return type;
2746 }
2747 *vendor = EAP_VENDOR_IETF;
2748 return EAP_TYPE_NONE;
2749 }
2750
2751
2752 /**
2753 * eap_get_phase2_types - Get list of allowed EAP phase 2 types
2754 * @config: Pointer to a network configuration
2755 * @count: Pointer to a variable to be filled with number of returned EAP types
2756 * Returns: Pointer to allocated type list or %NULL on failure
2757 *
2758 * This function generates an array of allowed EAP phase 2 (tunneled) types for
2759 * the given network configuration.
2760 */
eap_get_phase2_types(struct eap_peer_config * config,size_t * count)2761 struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config,
2762 size_t *count)
2763 {
2764 struct eap_method_type *buf;
2765 u32 method;
2766 int vendor;
2767 size_t mcount;
2768 const struct eap_method *methods, *m;
2769
2770 methods = eap_peer_get_methods(&mcount);
2771 if (methods == NULL)
2772 return NULL;
2773 *count = 0;
2774 buf = os_malloc(mcount * sizeof(struct eap_method_type));
2775 if (buf == NULL)
2776 return NULL;
2777
2778 for (m = methods; m; m = m->next) {
2779 vendor = m->vendor;
2780 method = m->method;
2781 if (eap_allowed_phase2_type(vendor, method)) {
2782 if (vendor == EAP_VENDOR_IETF &&
2783 method == EAP_TYPE_TLS && config &&
2784 !config->phase2_cert.private_key)
2785 continue;
2786 buf[*count].vendor = vendor;
2787 buf[*count].method = method;
2788 (*count)++;
2789 }
2790 }
2791
2792 return buf;
2793 }
2794
2795
2796 /**
2797 * eap_set_fast_reauth - Update fast_reauth setting
2798 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2799 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
2800 */
eap_set_fast_reauth(struct eap_sm * sm,int enabled)2801 void eap_set_fast_reauth(struct eap_sm *sm, int enabled)
2802 {
2803 sm->fast_reauth = enabled;
2804 }
2805
2806
2807 /**
2808 * eap_set_workaround - Update EAP workarounds setting
2809 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2810 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
2811 */
eap_set_workaround(struct eap_sm * sm,unsigned int workaround)2812 void eap_set_workaround(struct eap_sm *sm, unsigned int workaround)
2813 {
2814 sm->workaround = workaround;
2815 }
2816
2817
2818 /**
2819 * eap_get_config - Get current network configuration
2820 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2821 * Returns: Pointer to the current network configuration or %NULL if not found
2822 *
2823 * EAP peer methods should avoid using this function if they can use other
2824 * access functions, like eap_get_config_identity() and
2825 * eap_get_config_password(), that do not require direct access to
2826 * struct eap_peer_config.
2827 */
eap_get_config(struct eap_sm * sm)2828 struct eap_peer_config * eap_get_config(struct eap_sm *sm)
2829 {
2830 return sm->eapol_cb->get_config(sm->eapol_ctx);
2831 }
2832
2833
2834 /**
2835 * eap_get_config_identity - Get identity from the network configuration
2836 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2837 * @len: Buffer for the length of the identity
2838 * Returns: Pointer to the identity or %NULL if not found
2839 */
eap_get_config_identity(struct eap_sm * sm,size_t * len)2840 const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
2841 {
2842 struct eap_peer_config *config = eap_get_config(sm);
2843
2844 if (!config)
2845 return NULL;
2846
2847 if (sm->use_machine_cred) {
2848 *len = config->machine_identity_len;
2849 return config->machine_identity;
2850 }
2851
2852 *len = config->identity_len;
2853 return config->identity;
2854 }
2855
2856
2857 /**
2858 * eap_get_config_strict_conservative_peer_mode - get the value of
2859 * strict conservative peer mode in eap_peer_config.
2860 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2861 */
eap_get_config_strict_conservative_peer_mode(struct eap_sm * sm)2862 int eap_get_config_strict_conservative_peer_mode(struct eap_sm *sm)
2863 {
2864 struct eap_peer_config *config;
2865 config = eap_get_config(sm);
2866 if (config) {
2867 return config->strict_conservative_peer_mode;
2868 }
2869
2870 return 0;
2871 }
2872
2873
strnchr(const u8 * str,size_t len,u8 needle)2874 static const u8 * strnchr(const u8 *str, size_t len, u8 needle) {
2875 const u8 *cur = str;
2876
2877 if (NULL == str) return NULL;
2878 if (0 >= len) return NULL;
2879
2880 while (cur < str + len) {
2881 if (*cur == needle)
2882 return cur;
2883 cur++;
2884 }
2885 return NULL;
2886 }
2887
eap_get_config_realm(struct eap_sm * sm,size_t * len)2888 const u8 * eap_get_config_realm(struct eap_sm *sm, size_t *len) {
2889 struct eap_peer_config *config = eap_get_config(sm);
2890 const u8 *realm = NULL;
2891 size_t realm_len = 0;
2892 const u8 *identity = NULL;
2893 size_t identity_len = 0;
2894
2895 if (!config)
2896 return NULL;
2897
2898 /* Look for the realm of the permanent identity */
2899 identity = eap_get_config_identity(sm, &identity_len);
2900 realm = strnchr(identity, identity_len, '@');
2901 if (NULL != realm) {
2902 wpa_printf(MSG_DEBUG, "Get the realm from identity.");
2903 *len = identity_len - (realm - identity);
2904 return realm;
2905 }
2906
2907 /* Look for the realm of the anonymous identity. */
2908 identity = config->anonymous_identity;
2909 identity_len = config->anonymous_identity_len;
2910 realm = strnchr(identity, identity_len, '@');
2911 if (NULL != realm) {
2912 wpa_printf(MSG_DEBUG, "Get the realm from anonymous identity.");
2913 *len = identity_len - (realm - identity);
2914 return realm;
2915 }
2916
2917 /* Look for the realm of the real identity. */
2918 identity = config->imsi_identity;
2919 identity_len = config->imsi_identity_len;
2920 realm = strnchr(identity, identity_len, '@');
2921 if (NULL != realm) {
2922 wpa_printf(MSG_DEBUG, "Get the realm from IMSI identity.");
2923 *len = identity_len - (realm - identity);
2924 return realm;
2925 }
2926 wpa_printf(MSG_DEBUG, "No realm information in identities.");
2927 *len = 0;
2928 return NULL;
2929 }
2930
2931
eap_get_ext_password(struct eap_sm * sm,struct eap_peer_config * config)2932 static int eap_get_ext_password(struct eap_sm *sm,
2933 struct eap_peer_config *config)
2934 {
2935 char *name;
2936 const u8 *password;
2937 size_t password_len;
2938
2939 if (sm->use_machine_cred) {
2940 password = config->machine_password;
2941 password_len = config->machine_password_len;
2942 } else {
2943 password = config->password;
2944 password_len = config->password_len;
2945 }
2946
2947 if (!password)
2948 return -1;
2949
2950 name = os_zalloc(password_len + 1);
2951 if (!name)
2952 return -1;
2953 os_memcpy(name, password, password_len);
2954
2955 ext_password_free(sm->ext_pw_buf);
2956 sm->ext_pw_buf = ext_password_get(sm->ext_pw, name);
2957 os_free(name);
2958
2959 return sm->ext_pw_buf == NULL ? -1 : 0;
2960 }
2961
2962
2963 /**
2964 * eap_get_config_password - Get password from the network configuration
2965 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2966 * @len: Buffer for the length of the password
2967 * Returns: Pointer to the password or %NULL if not found
2968 */
eap_get_config_password(struct eap_sm * sm,size_t * len)2969 const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
2970 {
2971 struct eap_peer_config *config = eap_get_config(sm);
2972
2973 if (!config)
2974 return NULL;
2975
2976 if ((sm->use_machine_cred &&
2977 (config->flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD)) ||
2978 (!sm->use_machine_cred &&
2979 (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD))) {
2980 if (eap_get_ext_password(sm, config) < 0)
2981 return NULL;
2982 *len = wpabuf_len(sm->ext_pw_buf);
2983 return wpabuf_head(sm->ext_pw_buf);
2984 }
2985
2986 if (sm->use_machine_cred) {
2987 *len = config->machine_password_len;
2988 return config->machine_password;
2989 }
2990
2991 *len = config->password_len;
2992 return config->password;
2993 }
2994
2995
2996 /**
2997 * eap_get_config_password2 - Get password from the network configuration
2998 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2999 * @len: Buffer for the length of the password
3000 * @hash: Buffer for returning whether the password is stored as a
3001 * NtPasswordHash instead of plaintext password; can be %NULL if this
3002 * information is not needed
3003 * Returns: Pointer to the password or %NULL if not found
3004 */
eap_get_config_password2(struct eap_sm * sm,size_t * len,int * hash)3005 const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
3006 {
3007 struct eap_peer_config *config = eap_get_config(sm);
3008
3009 if (!config)
3010 return NULL;
3011
3012 if ((sm->use_machine_cred &&
3013 (config->flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD)) ||
3014 (!sm->use_machine_cred &&
3015 (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD))) {
3016 if (eap_get_ext_password(sm, config) < 0)
3017 return NULL;
3018 if (hash)
3019 *hash = 0;
3020 *len = wpabuf_len(sm->ext_pw_buf);
3021 return wpabuf_head(sm->ext_pw_buf);
3022 }
3023
3024 if (sm->use_machine_cred) {
3025 *len = config->machine_password_len;
3026 if (hash)
3027 *hash = !!(config->flags &
3028 EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH);
3029 return config->machine_password;
3030 }
3031
3032 *len = config->password_len;
3033 if (hash)
3034 *hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH);
3035 return config->password;
3036 }
3037
3038
3039 /**
3040 * eap_get_config_new_password - Get new password from network configuration
3041 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3042 * @len: Buffer for the length of the new password
3043 * Returns: Pointer to the new password or %NULL if not found
3044 */
eap_get_config_new_password(struct eap_sm * sm,size_t * len)3045 const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
3046 {
3047 struct eap_peer_config *config = eap_get_config(sm);
3048 if (config == NULL)
3049 return NULL;
3050 *len = config->new_password_len;
3051 return config->new_password;
3052 }
3053
3054
3055 /**
3056 * eap_get_config_otp - Get one-time password from the network configuration
3057 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3058 * @len: Buffer for the length of the one-time password
3059 * Returns: Pointer to the one-time password or %NULL if not found
3060 */
eap_get_config_otp(struct eap_sm * sm,size_t * len)3061 const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len)
3062 {
3063 struct eap_peer_config *config = eap_get_config(sm);
3064 if (config == NULL)
3065 return NULL;
3066 *len = config->otp_len;
3067 return config->otp;
3068 }
3069
3070
3071 /**
3072 * eap_clear_config_otp - Clear used one-time password
3073 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3074 *
3075 * This function clears a used one-time password (OTP) from the current network
3076 * configuration. This should be called when the OTP has been used and is not
3077 * needed anymore.
3078 */
eap_clear_config_otp(struct eap_sm * sm)3079 void eap_clear_config_otp(struct eap_sm *sm)
3080 {
3081 struct eap_peer_config *config = eap_get_config(sm);
3082 if (config == NULL)
3083 return;
3084 os_memset(config->otp, 0, config->otp_len);
3085 os_free(config->otp);
3086 config->otp = NULL;
3087 config->otp_len = 0;
3088 }
3089
3090
3091 /**
3092 * eap_get_config_phase1 - Get phase1 data from the network configuration
3093 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3094 * Returns: Pointer to the phase1 data or %NULL if not found
3095 */
eap_get_config_phase1(struct eap_sm * sm)3096 const char * eap_get_config_phase1(struct eap_sm *sm)
3097 {
3098 struct eap_peer_config *config = eap_get_config(sm);
3099 if (config == NULL)
3100 return NULL;
3101 return config->phase1;
3102 }
3103
3104
3105 /**
3106 * eap_get_config_phase2 - Get phase2 data from the network configuration
3107 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3108 * Returns: Pointer to the phase1 data or %NULL if not found
3109 */
eap_get_config_phase2(struct eap_sm * sm)3110 const char * eap_get_config_phase2(struct eap_sm *sm)
3111 {
3112 struct eap_peer_config *config = eap_get_config(sm);
3113 if (config == NULL)
3114 return NULL;
3115 return config->phase2;
3116 }
3117
3118
eap_get_config_fragment_size(struct eap_sm * sm)3119 int eap_get_config_fragment_size(struct eap_sm *sm)
3120 {
3121 struct eap_peer_config *config = eap_get_config(sm);
3122 if (config == NULL)
3123 return -1;
3124 return config->fragment_size;
3125 }
3126
3127
3128 /**
3129 * eap_key_available - Get key availability (eapKeyAvailable variable)
3130 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3131 * Returns: 1 if EAP keying material is available, 0 if not
3132 */
eap_key_available(struct eap_sm * sm)3133 int eap_key_available(struct eap_sm *sm)
3134 {
3135 return sm ? sm->eapKeyAvailable : 0;
3136 }
3137
3138 /**
3139 * eap_notify_permanent_id_req_denied - Notify that the AT_PERMANENT_ID_REQ
3140 * is denied from eap_peer when the strict conservative mode is enabled.
3141 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3142 */
eap_notify_permanent_id_req_denied(struct eap_sm * sm)3143 void eap_notify_permanent_id_req_denied(struct eap_sm *sm)
3144 {
3145 if (!sm || !sm->eapol_cb->notify_permanent_id_req_denied)
3146 return;
3147
3148 sm->eapol_cb->notify_permanent_id_req_denied(sm->eapol_ctx);
3149 }
3150
3151
3152 /**
3153 * eap_notify_success - Notify EAP state machine about external success trigger
3154 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3155 *
3156 * This function is called when external event, e.g., successful completion of
3157 * WPA-PSK key handshake, is indicating that EAP state machine should move to
3158 * success state. This is mainly used with security modes that do not use EAP
3159 * state machine (e.g., WPA-PSK).
3160 */
eap_notify_success(struct eap_sm * sm)3161 void eap_notify_success(struct eap_sm *sm)
3162 {
3163 if (sm) {
3164 sm->decision = DECISION_COND_SUCC;
3165 sm->EAP_state = EAP_SUCCESS;
3166 }
3167 }
3168
3169
3170 /**
3171 * eap_notify_lower_layer_success - Notification of lower layer success
3172 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3173 *
3174 * Notify EAP state machines that a lower layer has detected a successful
3175 * authentication. This is used to recover from dropped EAP-Success messages.
3176 */
eap_notify_lower_layer_success(struct eap_sm * sm)3177 void eap_notify_lower_layer_success(struct eap_sm *sm)
3178 {
3179 if (sm == NULL)
3180 return;
3181
3182 if (eapol_get_bool(sm, EAPOL_eapSuccess) ||
3183 sm->decision == DECISION_FAIL ||
3184 (sm->methodState != METHOD_MAY_CONT &&
3185 sm->methodState != METHOD_DONE))
3186 return;
3187
3188 if (sm->eapKeyData != NULL)
3189 sm->eapKeyAvailable = true;
3190 eapol_set_bool(sm, EAPOL_eapSuccess, true);
3191 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
3192 "EAP authentication completed successfully (based on lower "
3193 "layer success)");
3194 }
3195
3196
3197 /**
3198 * eap_get_eapSessionId - Get Session-Id from EAP state machine
3199 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3200 * @len: Pointer to variable that will be set to number of bytes in the session
3201 * Returns: Pointer to the EAP Session-Id or %NULL on failure
3202 *
3203 * Fetch EAP Session-Id from the EAP state machine. The Session-Id is available
3204 * only after a successful authentication. EAP state machine continues to manage
3205 * the Session-Id and the caller must not change or free the returned data.
3206 */
eap_get_eapSessionId(struct eap_sm * sm,size_t * len)3207 const u8 * eap_get_eapSessionId(struct eap_sm *sm, size_t *len)
3208 {
3209 if (sm == NULL || sm->eapSessionId == NULL) {
3210 *len = 0;
3211 return NULL;
3212 }
3213
3214 *len = sm->eapSessionIdLen;
3215 return sm->eapSessionId;
3216 }
3217
3218
3219 /**
3220 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
3221 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3222 * @len: Pointer to variable that will be set to number of bytes in the key
3223 * Returns: Pointer to the EAP keying data or %NULL on failure
3224 *
3225 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
3226 * key is available only after a successful authentication. EAP state machine
3227 * continues to manage the key data and the caller must not change or free the
3228 * returned data.
3229 */
eap_get_eapKeyData(struct eap_sm * sm,size_t * len)3230 const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len)
3231 {
3232 if (sm == NULL || sm->eapKeyData == NULL) {
3233 *len = 0;
3234 return NULL;
3235 }
3236
3237 *len = sm->eapKeyDataLen;
3238 return sm->eapKeyData;
3239 }
3240
3241
3242 /**
3243 * eap_get_eapKeyData - Get EAP response data
3244 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3245 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
3246 *
3247 * Fetch EAP response (eapRespData) from the EAP state machine. This data is
3248 * available when EAP state machine has processed an incoming EAP request. The
3249 * EAP state machine does not maintain a reference to the response after this
3250 * function is called and the caller is responsible for freeing the data.
3251 */
eap_get_eapRespData(struct eap_sm * sm)3252 struct wpabuf * eap_get_eapRespData(struct eap_sm *sm)
3253 {
3254 struct wpabuf *resp;
3255
3256 if (sm == NULL || sm->eapRespData == NULL)
3257 return NULL;
3258
3259 resp = sm->eapRespData;
3260 sm->eapRespData = NULL;
3261
3262 return resp;
3263 }
3264
3265
3266 /**
3267 * eap_sm_register_scard_ctx - Notification of smart card context
3268 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3269 * @ctx: Context data for smart card operations
3270 *
3271 * Notify EAP state machines of context data for smart card operations. This
3272 * context data will be used as a parameter for scard_*() functions.
3273 */
eap_register_scard_ctx(struct eap_sm * sm,void * ctx)3274 void eap_register_scard_ctx(struct eap_sm *sm, void *ctx)
3275 {
3276 if (sm)
3277 sm->scard_ctx = ctx;
3278 }
3279
3280
3281 /**
3282 * eap_set_config_blob - Set or add a named configuration blob
3283 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3284 * @blob: New value for the blob
3285 *
3286 * Adds a new configuration blob or replaces the current value of an existing
3287 * blob.
3288 */
eap_set_config_blob(struct eap_sm * sm,struct wpa_config_blob * blob)3289 void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob)
3290 {
3291 #ifndef CONFIG_NO_CONFIG_BLOBS
3292 sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob);
3293 #endif /* CONFIG_NO_CONFIG_BLOBS */
3294 }
3295
3296
3297 /**
3298 * eap_get_config_blob - Get a named configuration blob
3299 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3300 * @name: Name of the blob
3301 * Returns: Pointer to blob data or %NULL if not found
3302 */
eap_get_config_blob(struct eap_sm * sm,const char * name)3303 const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm,
3304 const char *name)
3305 {
3306 #ifndef CONFIG_NO_CONFIG_BLOBS
3307 return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name);
3308 #else /* CONFIG_NO_CONFIG_BLOBS */
3309 return NULL;
3310 #endif /* CONFIG_NO_CONFIG_BLOBS */
3311 }
3312
3313
3314 /**
3315 * eap_set_force_disabled - Set force_disabled flag
3316 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3317 * @disabled: 1 = EAP disabled, 0 = EAP enabled
3318 *
3319 * This function is used to force EAP state machine to be disabled when it is
3320 * not in use (e.g., with WPA-PSK or plaintext connections).
3321 */
eap_set_force_disabled(struct eap_sm * sm,int disabled)3322 void eap_set_force_disabled(struct eap_sm *sm, int disabled)
3323 {
3324 sm->force_disabled = disabled;
3325 }
3326
3327
3328 /**
3329 * eap_set_external_sim - Set external_sim flag
3330 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3331 * @external_sim: Whether external SIM/USIM processing is used
3332 */
eap_set_external_sim(struct eap_sm * sm,int external_sim)3333 void eap_set_external_sim(struct eap_sm *sm, int external_sim)
3334 {
3335 sm->external_sim = external_sim;
3336 }
3337
3338
3339 /**
3340 * eap_notify_pending - Notify that EAP method is ready to re-process a request
3341 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3342 *
3343 * An EAP method can perform a pending operation (e.g., to get a response from
3344 * an external process). Once the response is available, this function can be
3345 * used to request EAPOL state machine to retry delivering the previously
3346 * received (and still unanswered) EAP request to EAP state machine.
3347 */
eap_notify_pending(struct eap_sm * sm)3348 void eap_notify_pending(struct eap_sm *sm)
3349 {
3350 sm->eapol_cb->notify_pending(sm->eapol_ctx);
3351 }
3352
3353
3354 /**
3355 * eap_invalidate_cached_session - Mark cached session data invalid
3356 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3357 */
eap_invalidate_cached_session(struct eap_sm * sm)3358 void eap_invalidate_cached_session(struct eap_sm *sm)
3359 {
3360 if (sm)
3361 eap_deinit_prev_method(sm, "invalidate");
3362 }
3363
3364
eap_is_wps_pbc_enrollee(struct eap_peer_config * conf)3365 int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf)
3366 {
3367 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
3368 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
3369 return 0; /* Not a WPS Enrollee */
3370
3371 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL)
3372 return 0; /* Not using PBC */
3373
3374 return 1;
3375 }
3376
3377
eap_is_wps_pin_enrollee(struct eap_peer_config * conf)3378 int eap_is_wps_pin_enrollee(struct eap_peer_config *conf)
3379 {
3380 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
3381 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
3382 return 0; /* Not a WPS Enrollee */
3383
3384 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL)
3385 return 0; /* Not using PIN */
3386
3387 return 1;
3388 }
3389
3390
eap_sm_set_ext_pw_ctx(struct eap_sm * sm,struct ext_password_data * ext)3391 void eap_sm_set_ext_pw_ctx(struct eap_sm *sm, struct ext_password_data *ext)
3392 {
3393 ext_password_free(sm->ext_pw_buf);
3394 sm->ext_pw_buf = NULL;
3395 sm->ext_pw = ext;
3396 }
3397
3398
3399 /**
3400 * eap_set_anon_id - Set or add anonymous identity
3401 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3402 * @id: Anonymous identity (e.g., EAP-SIM pseudonym) or %NULL to clear
3403 * @len: Length of anonymous identity in octets
3404 */
eap_set_anon_id(struct eap_sm * sm,const u8 * id,size_t len)3405 void eap_set_anon_id(struct eap_sm *sm, const u8 *id, size_t len)
3406 {
3407 if (sm->eapol_cb->set_anon_id)
3408 sm->eapol_cb->set_anon_id(sm->eapol_ctx, id, len);
3409 }
3410
3411
eap_peer_was_failure_expected(struct eap_sm * sm)3412 int eap_peer_was_failure_expected(struct eap_sm *sm)
3413 {
3414 return sm->expected_failure;
3415 }
3416