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