blob: 4a0990889ad39bd753705a3476322c86267af264 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * EAP peer state machines (RFC 4137)
Hai Shalomc3565922019-10-28 11:58:20 -07003 * Copyright (c) 2004-2019, 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
Hai Shalomc3565922019-10-28 11:58:20 -070035#define EAP_MAX_AUTH_ROUNDS 100
36#define EAP_MAX_AUTH_ROUNDS_SHORT 50
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#define EAP_CLIENT_TIMEOUT_DEFAULT 60
38
39
Hai Shalome21d4e82020-04-29 16:34:06 -070040static bool eap_sm_allowMethod(struct eap_sm *sm, int vendor,
41 enum eap_type method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id);
43static void eap_sm_processIdentity(struct eap_sm *sm,
44 const struct wpabuf *req);
45static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req);
46static struct wpabuf * eap_sm_buildNotify(int id);
47static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req);
48#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
49static const char * eap_sm_method_state_txt(EapMethodState state);
50static const char * eap_sm_decision_txt(EapDecision decision);
51#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -080052static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field,
53 const char *msg, size_t msglen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054
55
56
Hai Shalome21d4e82020-04-29 16:34:06 -070057static bool eapol_get_bool(struct eap_sm *sm, enum eapol_bool_var var)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058{
59 return sm->eapol_cb->get_bool(sm->eapol_ctx, var);
60}
61
62
63static void eapol_set_bool(struct eap_sm *sm, enum eapol_bool_var var,
Hai Shalome21d4e82020-04-29 16:34:06 -070064 bool value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065{
66 sm->eapol_cb->set_bool(sm->eapol_ctx, var, value);
67}
68
69
70static unsigned int eapol_get_int(struct eap_sm *sm, enum eapol_int_var var)
71{
72 return sm->eapol_cb->get_int(sm->eapol_ctx, var);
73}
74
75
76static void eapol_set_int(struct eap_sm *sm, enum eapol_int_var var,
77 unsigned int value)
78{
79 sm->eapol_cb->set_int(sm->eapol_ctx, var, value);
80}
81
82
83static struct wpabuf * eapol_get_eapReqData(struct eap_sm *sm)
84{
85 return sm->eapol_cb->get_eapReqData(sm->eapol_ctx);
86}
87
88
Dmitry Shmidt04949592012-07-19 12:16:46 -070089static void eap_notify_status(struct eap_sm *sm, const char *status,
90 const char *parameter)
91{
92 wpa_printf(MSG_DEBUG, "EAP: Status notification: %s (param=%s)",
93 status, parameter);
94 if (sm->eapol_cb->notify_status)
95 sm->eapol_cb->notify_status(sm->eapol_ctx, status, parameter);
96}
97
Roshan Pius3a1667e2018-07-03 15:17:14 -070098
Ahmed ElArabawy9c86a7f2018-03-15 09:00:10 -070099static void eap_report_error(struct eap_sm *sm, int error_code)
100{
101 wpa_printf(MSG_DEBUG, "EAP: Error notification: %d", error_code);
102 if (sm->eapol_cb->notify_eap_error)
103 sm->eapol_cb->notify_eap_error(sm->eapol_ctx, error_code);
104}
Dmitry Shmidt04949592012-07-19 12:16:46 -0700105
Roshan Pius3a1667e2018-07-03 15:17:14 -0700106
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700107static void eap_sm_free_key(struct eap_sm *sm)
108{
109 if (sm->eapKeyData) {
110 bin_clear_free(sm->eapKeyData, sm->eapKeyDataLen);
111 sm->eapKeyData = NULL;
112 }
113}
114
115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116static void eap_deinit_prev_method(struct eap_sm *sm, const char *txt)
117{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118 ext_password_free(sm->ext_pw_buf);
119 sm->ext_pw_buf = NULL;
120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 if (sm->m == NULL || sm->eap_method_priv == NULL)
122 return;
123
124 wpa_printf(MSG_DEBUG, "EAP: deinitialize previously used EAP method "
125 "(%d, %s) at %s", sm->selectedMethod, sm->m->name, txt);
126 sm->m->deinit(sm, sm->eap_method_priv);
127 sm->eap_method_priv = NULL;
128 sm->m = NULL;
129}
130
131
132/**
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700133 * eap_config_allowed_method - Check whether EAP method is allowed
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700135 * @config: EAP configuration
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136 * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
137 * @method: EAP type
138 * Returns: 1 = allowed EAP method, 0 = not allowed
139 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700140static int eap_config_allowed_method(struct eap_sm *sm,
141 struct eap_peer_config *config,
142 int vendor, u32 method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700144 int i;
145 struct eap_method_type *m;
146
147 if (config == NULL || config->eap_methods == NULL)
148 return 1;
149
150 m = config->eap_methods;
151 for (i = 0; m[i].vendor != EAP_VENDOR_IETF ||
152 m[i].method != EAP_TYPE_NONE; i++) {
153 if (m[i].vendor == vendor && m[i].method == method)
154 return 1;
155 }
156 return 0;
157}
158
159
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700160/**
161 * eap_allowed_method - Check whether EAP method is allowed
162 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
163 * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
164 * @method: EAP type
165 * Returns: 1 = allowed EAP method, 0 = not allowed
166 */
167int eap_allowed_method(struct eap_sm *sm, int vendor, u32 method)
168{
169 return eap_config_allowed_method(sm, eap_get_config(sm), vendor,
170 method);
171}
172
173
174#if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
175static int eap_sm_append_3gpp_realm(struct eap_sm *sm, char *imsi,
176 size_t max_len, size_t *imsi_len,
177 int mnc_len)
178{
179 char *pos, mnc[4];
180
181 if (*imsi_len + 36 > max_len) {
182 wpa_printf(MSG_WARNING, "No room for realm in IMSI buffer");
183 return -1;
184 }
185
186 if (mnc_len != 2 && mnc_len != 3)
187 mnc_len = 3;
188
189 if (mnc_len == 2) {
190 mnc[0] = '0';
191 mnc[1] = imsi[3];
192 mnc[2] = imsi[4];
193 } else if (mnc_len == 3) {
194 mnc[0] = imsi[3];
195 mnc[1] = imsi[4];
196 mnc[2] = imsi[5];
197 }
198 mnc[3] = '\0';
199
200 pos = imsi + *imsi_len;
201 pos += os_snprintf(pos, imsi + max_len - pos,
202 "@wlan.mnc%s.mcc%c%c%c.3gppnetwork.org",
203 mnc, imsi[0], imsi[1], imsi[2]);
204 *imsi_len = pos - imsi;
205
206 return 0;
207}
208#endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
209
210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211/*
212 * This state initializes state machine variables when the machine is
Hai Shalome21d4e82020-04-29 16:34:06 -0700213 * activated (portEnabled = true). This is also used when re-starting
214 * authentication (eapRestart == true).
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 */
216SM_STATE(EAP, INITIALIZE)
217{
218 SM_ENTRY(EAP, INITIALIZE);
219 if (sm->fast_reauth && sm->m && sm->m->has_reauth_data &&
220 sm->m->has_reauth_data(sm, sm->eap_method_priv) &&
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700221 !sm->prev_failure &&
222 sm->last_config == eap_get_config(sm)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223 wpa_printf(MSG_DEBUG, "EAP: maintaining EAP method data for "
224 "fast reauthentication");
225 sm->m->deinit_for_reauth(sm, sm->eap_method_priv);
226 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700227 sm->last_config = eap_get_config(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 eap_deinit_prev_method(sm, "INITIALIZE");
229 }
230 sm->selectedMethod = EAP_TYPE_NONE;
231 sm->methodState = METHOD_NONE;
Hai Shalome21d4e82020-04-29 16:34:06 -0700232 sm->allowNotifications = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233 sm->decision = DECISION_FAIL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800234 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
Hai Shalome21d4e82020-04-29 16:34:06 -0700236 eapol_set_bool(sm, EAPOL_eapSuccess, false);
237 eapol_set_bool(sm, EAPOL_eapFail, false);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700238 eap_sm_free_key(sm);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800239 os_free(sm->eapSessionId);
240 sm->eapSessionId = NULL;
Hai Shalome21d4e82020-04-29 16:34:06 -0700241 sm->eapKeyAvailable = false;
242 eapol_set_bool(sm, EAPOL_eapRestart, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 sm->lastId = -1; /* new session - make sure this does not match with
244 * the first EAP-Packet */
245 /*
246 * RFC 4137 does not reset eapResp and eapNoResp here. However, this
247 * seemed to be able to trigger cases where both were set and if EAPOL
248 * state machine uses eapNoResp first, it may end up not sending a real
249 * reply correctly. This occurred when the workaround in FAIL state set
Hai Shalome21d4e82020-04-29 16:34:06 -0700250 * eapNoResp = true.. Maybe that workaround needs to be fixed to do
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251 * something else(?)
252 */
Hai Shalome21d4e82020-04-29 16:34:06 -0700253 eapol_set_bool(sm, EAPOL_eapResp, false);
254 eapol_set_bool(sm, EAPOL_eapNoResp, false);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800255 /*
256 * RFC 4137 does not reset ignore here, but since it is possible for
Hai Shalome21d4e82020-04-29 16:34:06 -0700257 * some method code paths to end up not setting ignore=false, clear the
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800258 * value here to avoid issues if a previous authentication attempt
Hai Shalome21d4e82020-04-29 16:34:06 -0700259 * failed with ignore=true being left behind in the last
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800260 * m.check(eapReqData) operation.
261 */
262 sm->ignore = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263 sm->num_rounds = 0;
Hai Shalomc3565922019-10-28 11:58:20 -0700264 sm->num_rounds_short = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 sm->prev_failure = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800266 sm->expected_failure = 0;
Hai Shalome21d4e82020-04-29 16:34:06 -0700267 sm->reauthInit = false;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800268 sm->erp_seq = (u32) -1;
Hai Shalomc3565922019-10-28 11:58:20 -0700269 sm->use_machine_cred = 0;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000270 sm->eap_fast_mschapv2 = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271}
272
273
274/*
275 * This state is reached whenever service from the lower layer is interrupted
Hai Shalome21d4e82020-04-29 16:34:06 -0700276 * or unavailable (portEnabled == false). Immediate transition to INITIALIZE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 * occurs when the port becomes enabled.
278 */
279SM_STATE(EAP, DISABLED)
280{
281 SM_ENTRY(EAP, DISABLED);
282 sm->num_rounds = 0;
Hai Shalomc3565922019-10-28 11:58:20 -0700283 sm->num_rounds_short = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700284 /*
285 * RFC 4137 does not describe clearing of idleWhile here, but doing so
286 * allows the timer tick to be stopped more quickly when EAP is not in
287 * use.
288 */
289 eapol_set_int(sm, EAPOL_idleWhile, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290}
291
292
293/*
294 * The state machine spends most of its time here, waiting for something to
295 * happen. This state is entered unconditionally from INITIALIZE, DISCARD, and
296 * SEND_RESPONSE states.
297 */
298SM_STATE(EAP, IDLE)
299{
300 SM_ENTRY(EAP, IDLE);
301}
302
303
304/*
Hai Shalome21d4e82020-04-29 16:34:06 -0700305 * This state is entered when an EAP packet is received (eapReq == true) to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 * parse the packet header.
307 */
308SM_STATE(EAP, RECEIVED)
309{
310 const struct wpabuf *eapReqData;
311
312 SM_ENTRY(EAP, RECEIVED);
313 eapReqData = eapol_get_eapReqData(sm);
314 /* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */
315 eap_sm_parseEapReq(sm, eapReqData);
316 sm->num_rounds++;
Hai Shalomc3565922019-10-28 11:58:20 -0700317 if (!eapReqData || wpabuf_len(eapReqData) < 20)
318 sm->num_rounds_short++;
319 else
320 sm->num_rounds_short = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321}
322
323
324/*
325 * This state is entered when a request for a new type comes in. Either the
326 * correct method is started, or a Nak response is built.
327 */
328SM_STATE(EAP, GET_METHOD)
329{
330 int reinit;
Hai Shalomc3565922019-10-28 11:58:20 -0700331 enum eap_type method;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700332 const struct eap_method *eap_method;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333
334 SM_ENTRY(EAP, GET_METHOD);
335
336 if (sm->reqMethod == EAP_TYPE_EXPANDED)
337 method = sm->reqVendorMethod;
338 else
339 method = sm->reqMethod;
340
Dmitry Shmidt04949592012-07-19 12:16:46 -0700341 eap_method = eap_peer_get_eap_method(sm->reqVendor, method);
342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 if (!eap_sm_allowMethod(sm, sm->reqVendor, method)) {
344 wpa_printf(MSG_DEBUG, "EAP: vendor %u method %u not allowed",
345 sm->reqVendor, method);
346 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
347 "vendor=%u method=%u -> NAK",
348 sm->reqVendor, method);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700349 eap_notify_status(sm, "refuse proposed method",
350 eap_method ? eap_method->name : "unknown");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 goto nak;
352 }
353
354 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
355 "vendor=%u method=%u", sm->reqVendor, method);
356
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357 eap_notify_status(sm, "accept proposed method",
358 eap_method ? eap_method->name : "unknown");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359 /*
360 * RFC 4137 does not define specific operation for fast
361 * re-authentication (session resumption). The design here is to allow
362 * the previously used method data to be maintained for
363 * re-authentication if the method support session resumption.
364 * Otherwise, the previously used method data is freed and a new method
365 * is allocated here.
366 */
367 if (sm->fast_reauth &&
368 sm->m && sm->m->vendor == sm->reqVendor &&
369 sm->m->method == method &&
370 sm->m->has_reauth_data &&
371 sm->m->has_reauth_data(sm, sm->eap_method_priv)) {
372 wpa_printf(MSG_DEBUG, "EAP: Using previous method data"
373 " for fast re-authentication");
374 reinit = 1;
375 } else {
376 eap_deinit_prev_method(sm, "GET_METHOD");
377 reinit = 0;
378 }
379
380 sm->selectedMethod = sm->reqMethod;
381 if (sm->m == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700382 sm->m = eap_method;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 if (!sm->m) {
384 wpa_printf(MSG_DEBUG, "EAP: Could not find selected method: "
385 "vendor %d method %d",
386 sm->reqVendor, method);
387 goto nak;
388 }
389
390 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
391
392 wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP method: "
393 "vendor %u method %u (%s)",
394 sm->reqVendor, method, sm->m->name);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800395 if (reinit) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396 sm->eap_method_priv = sm->m->init_for_reauth(
397 sm, sm->eap_method_priv);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800398 } else {
399 sm->waiting_ext_cert_check = 0;
400 sm->ext_cert_check = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 sm->eap_method_priv = sm->m->init(sm);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800402 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700403
404 if (sm->eap_method_priv == NULL) {
405 struct eap_peer_config *config = eap_get_config(sm);
406 wpa_msg(sm->msg_ctx, MSG_INFO,
407 "EAP: Failed to initialize EAP method: vendor %u "
408 "method %u (%s)",
409 sm->reqVendor, method, sm->m->name);
410 sm->m = NULL;
411 sm->methodState = METHOD_NONE;
412 sm->selectedMethod = EAP_TYPE_NONE;
413 if (sm->reqMethod == EAP_TYPE_TLS && config &&
414 (config->pending_req_pin ||
415 config->pending_req_passphrase)) {
416 /*
417 * Return without generating Nak in order to allow
418 * entering of PIN code or passphrase to retry the
419 * current EAP packet.
420 */
421 wpa_printf(MSG_DEBUG, "EAP: Pending PIN/passphrase "
422 "request - skip Nak");
423 return;
424 }
425
426 goto nak;
427 }
428
429 sm->methodState = METHOD_INIT;
430 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_METHOD
431 "EAP vendor %u method %u (%s) selected",
432 sm->reqVendor, method, sm->m->name);
Gabriel Biren3a2ec2c2022-03-07 17:59:41 +0000433
434 if (sm->eapol_cb->notify_eap_method_selected) {
435 char *format_str = "EAP vendor %u method %u (%s) selected";
436 int msg_len = snprintf(NULL, 0, format_str,
437 sm->reqVendor, method, sm->m->name) + 1;
438 char *msg = os_malloc(msg_len);
439 snprintf(msg, msg_len, format_str,
440 sm->reqVendor, method, sm->m->name);
441 sm->eapol_cb->notify_eap_method_selected(sm->eapol_ctx, msg);
442 os_free(msg);
443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 return;
445
446nak:
447 wpabuf_free(sm->eapRespData);
448 sm->eapRespData = NULL;
449 sm->eapRespData = eap_sm_buildNak(sm, sm->reqId);
450}
451
452
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800453#ifdef CONFIG_ERP
454
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700455static char * eap_get_realm(struct eap_sm *sm, struct eap_peer_config *config)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800456{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800457 char *realm;
458 size_t i, realm_len;
459
460 if (!config)
461 return NULL;
462
463 if (config->identity) {
464 for (i = 0; i < config->identity_len; i++) {
465 if (config->identity[i] == '@')
466 break;
467 }
468 if (i < config->identity_len) {
469 realm_len = config->identity_len - i - 1;
470 realm = os_malloc(realm_len + 1);
471 if (realm == NULL)
472 return NULL;
473 os_memcpy(realm, &config->identity[i + 1], realm_len);
474 realm[realm_len] = '\0';
475 return realm;
476 }
477 }
478
479 if (config->anonymous_identity) {
480 for (i = 0; i < config->anonymous_identity_len; i++) {
481 if (config->anonymous_identity[i] == '@')
482 break;
483 }
484 if (i < config->anonymous_identity_len) {
485 realm_len = config->anonymous_identity_len - i - 1;
486 realm = os_malloc(realm_len + 1);
487 if (realm == NULL)
488 return NULL;
489 os_memcpy(realm, &config->anonymous_identity[i + 1],
490 realm_len);
491 realm[realm_len] = '\0';
492 return realm;
493 }
494 }
495
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700496#ifdef CONFIG_EAP_PROXY
497 /* When identity is not provided in the config, build the realm from
498 * IMSI for eap_proxy based methods.
499 */
500 if (!config->identity && !config->anonymous_identity &&
501 sm->eapol_cb->get_imsi &&
502 (eap_config_allowed_method(sm, config, EAP_VENDOR_IETF,
503 EAP_TYPE_SIM) ||
504 eap_config_allowed_method(sm, config, EAP_VENDOR_IETF,
505 EAP_TYPE_AKA) ||
506 eap_config_allowed_method(sm, config, EAP_VENDOR_IETF,
507 EAP_TYPE_AKA_PRIME))) {
508 char imsi[100];
509 size_t imsi_len;
510 int mnc_len, pos;
511
512 wpa_printf(MSG_DEBUG, "EAP: Build realm from IMSI (eap_proxy)");
513 mnc_len = sm->eapol_cb->get_imsi(sm->eapol_ctx, config->sim_num,
514 imsi, &imsi_len);
515 if (mnc_len < 0)
516 return NULL;
517
518 pos = imsi_len + 1; /* points to the beginning of the realm */
519 if (eap_sm_append_3gpp_realm(sm, imsi, sizeof(imsi), &imsi_len,
520 mnc_len) < 0) {
521 wpa_printf(MSG_WARNING, "Could not append realm");
522 return NULL;
523 }
524
525 realm = os_strdup(&imsi[pos]);
526 if (!realm)
527 return NULL;
528
529 wpa_printf(MSG_DEBUG, "EAP: Generated realm '%s'", realm);
530 return realm;
531 }
532#endif /* CONFIG_EAP_PROXY */
533
534 return NULL;
535}
536
537
538static char * eap_home_realm(struct eap_sm *sm)
539{
540 return eap_get_realm(sm, eap_get_config(sm));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800541}
542
543
544static struct eap_erp_key *
545eap_erp_get_key(struct eap_sm *sm, const char *realm)
546{
547 struct eap_erp_key *erp;
548
549 dl_list_for_each(erp, &sm->erp_keys, struct eap_erp_key, list) {
550 char *pos;
551
552 pos = os_strchr(erp->keyname_nai, '@');
553 if (!pos)
554 continue;
555 pos++;
556 if (os_strcmp(pos, realm) == 0)
557 return erp;
558 }
559
560 return NULL;
561}
562
563
564static struct eap_erp_key *
565eap_erp_get_key_nai(struct eap_sm *sm, const char *nai)
566{
567 struct eap_erp_key *erp;
568
569 dl_list_for_each(erp, &sm->erp_keys, struct eap_erp_key, list) {
570 if (os_strcmp(erp->keyname_nai, nai) == 0)
571 return erp;
572 }
573
574 return NULL;
575}
576
577
578static void eap_peer_erp_free_key(struct eap_erp_key *erp)
579{
580 dl_list_del(&erp->list);
581 bin_clear_free(erp, sizeof(*erp));
582}
583
584
585static void eap_erp_remove_keys_realm(struct eap_sm *sm, const char *realm)
586{
587 struct eap_erp_key *erp;
588
589 while ((erp = eap_erp_get_key(sm, realm)) != NULL) {
590 wpa_printf(MSG_DEBUG, "EAP: Delete old ERP key %s",
591 erp->keyname_nai);
592 eap_peer_erp_free_key(erp);
593 }
594}
595
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700596
597int eap_peer_update_erp_next_seq_num(struct eap_sm *sm, u16 next_seq_num)
598{
599 struct eap_erp_key *erp;
600 char *home_realm;
601
602 home_realm = eap_home_realm(sm);
603 if (!home_realm || os_strlen(home_realm) == 0) {
604 os_free(home_realm);
605 return -1;
606 }
607
608 erp = eap_erp_get_key(sm, home_realm);
609 if (!erp) {
610 wpa_printf(MSG_DEBUG,
611 "EAP: Failed to find ERP key for realm: %s",
612 home_realm);
613 os_free(home_realm);
614 return -1;
615 }
616
617 if ((u32) next_seq_num < erp->next_seq) {
618 /* Sequence number has wrapped around, clear this ERP
619 * info and do a full auth next time.
620 */
621 eap_peer_erp_free_key(erp);
622 } else {
623 erp->next_seq = (u32) next_seq_num;
624 }
625
626 os_free(home_realm);
627 return 0;
628}
629
630
631int eap_peer_get_erp_info(struct eap_sm *sm, struct eap_peer_config *config,
632 const u8 **username, size_t *username_len,
633 const u8 **realm, size_t *realm_len,
634 u16 *erp_next_seq_num, const u8 **rrk,
635 size_t *rrk_len)
636{
637 struct eap_erp_key *erp;
638 char *home_realm;
639 char *pos;
640
641 if (config)
642 home_realm = eap_get_realm(sm, config);
643 else
644 home_realm = eap_home_realm(sm);
645 if (!home_realm || os_strlen(home_realm) == 0) {
646 os_free(home_realm);
647 return -1;
648 }
649
650 erp = eap_erp_get_key(sm, home_realm);
651 os_free(home_realm);
652 if (!erp)
653 return -1;
654
655 if (erp->next_seq >= 65536)
656 return -1; /* SEQ has range of 0..65535 */
657
658 pos = os_strchr(erp->keyname_nai, '@');
659 if (!pos)
660 return -1; /* this cannot really happen */
661 *username_len = pos - erp->keyname_nai;
662 *username = (u8 *) erp->keyname_nai;
663
664 pos++;
665 *realm_len = os_strlen(pos);
666 *realm = (u8 *) pos;
667
668 *erp_next_seq_num = (u16) erp->next_seq;
669
670 *rrk_len = erp->rRK_len;
671 *rrk = erp->rRK;
672
673 if (*username_len == 0 || *realm_len == 0 || *rrk_len == 0)
674 return -1;
675
676 return 0;
677}
678
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800679#endif /* CONFIG_ERP */
680
681
682void eap_peer_erp_free_keys(struct eap_sm *sm)
683{
684#ifdef CONFIG_ERP
685 struct eap_erp_key *erp, *tmp;
686
687 dl_list_for_each_safe(erp, tmp, &sm->erp_keys, struct eap_erp_key, list)
688 eap_peer_erp_free_key(erp);
689#endif /* CONFIG_ERP */
690}
691
692
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800693/* Note: If ext_session and/or ext_emsk are passed to this function, they are
694 * expected to point to allocated memory and those allocations will be freed
695 * unconditionally. */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700696void eap_peer_erp_init(struct eap_sm *sm, u8 *ext_session_id,
697 size_t ext_session_id_len, u8 *ext_emsk,
698 size_t ext_emsk_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800699{
700#ifdef CONFIG_ERP
701 u8 *emsk = NULL;
702 size_t emsk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700703 u8 *session_id = NULL;
704 size_t session_id_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800705 u8 EMSKname[EAP_EMSK_NAME_LEN];
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800706 u8 len[2], ctx[3];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800707 char *realm;
708 size_t realm_len, nai_buf_len;
709 struct eap_erp_key *erp = NULL;
710 int pos;
711
712 realm = eap_home_realm(sm);
713 if (!realm)
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800714 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800715 realm_len = os_strlen(realm);
716 wpa_printf(MSG_DEBUG, "EAP: Realm for ERP keyName-NAI: %s", realm);
717 eap_erp_remove_keys_realm(sm, realm);
718
719 nai_buf_len = 2 * EAP_EMSK_NAME_LEN + 1 + realm_len;
720 if (nai_buf_len > 253) {
721 /*
722 * keyName-NAI has a maximum length of 253 octet to fit in
723 * RADIUS attributes.
724 */
725 wpa_printf(MSG_DEBUG,
726 "EAP: Too long realm for ERP keyName-NAI maximum length");
727 goto fail;
728 }
729 nai_buf_len++; /* null termination */
730 erp = os_zalloc(sizeof(*erp) + nai_buf_len);
731 if (erp == NULL)
732 goto fail;
733
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700734 if (ext_emsk) {
735 emsk = ext_emsk;
736 emsk_len = ext_emsk_len;
737 } else {
738 emsk = sm->m->get_emsk(sm, sm->eap_method_priv, &emsk_len);
739 }
740
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800741 if (!emsk || emsk_len == 0 || emsk_len > ERP_MAX_KEY_LEN) {
742 wpa_printf(MSG_DEBUG,
743 "EAP: No suitable EMSK available for ERP");
744 goto fail;
745 }
746
747 wpa_hexdump_key(MSG_DEBUG, "EAP: EMSK", emsk, emsk_len);
748
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700749 if (ext_session_id) {
750 session_id = ext_session_id;
751 session_id_len = ext_session_id_len;
752 } else {
753 session_id = sm->eapSessionId;
754 session_id_len = sm->eapSessionIdLen;
755 }
756
757 if (!session_id || session_id_len == 0) {
758 wpa_printf(MSG_DEBUG,
759 "EAP: No suitable session id available for ERP");
760 goto fail;
761 }
762
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800763 WPA_PUT_BE16(len, EAP_EMSK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700764 if (hmac_sha256_kdf(session_id, session_id_len, "EMSK", len,
765 sizeof(len), EMSKname, EAP_EMSK_NAME_LEN) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800766 wpa_printf(MSG_DEBUG, "EAP: Could not derive EMSKname");
767 goto fail;
768 }
769 wpa_hexdump(MSG_DEBUG, "EAP: EMSKname", EMSKname, EAP_EMSK_NAME_LEN);
770
771 pos = wpa_snprintf_hex(erp->keyname_nai, nai_buf_len,
772 EMSKname, EAP_EMSK_NAME_LEN);
773 erp->keyname_nai[pos] = '@';
774 os_memcpy(&erp->keyname_nai[pos + 1], realm, realm_len);
775
776 WPA_PUT_BE16(len, emsk_len);
777 if (hmac_sha256_kdf(emsk, emsk_len,
778 "EAP Re-authentication Root Key@ietf.org",
779 len, sizeof(len), erp->rRK, emsk_len) < 0) {
780 wpa_printf(MSG_DEBUG, "EAP: Could not derive rRK for ERP");
781 goto fail;
782 }
783 erp->rRK_len = emsk_len;
784 wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rRK", erp->rRK, erp->rRK_len);
785
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800786 ctx[0] = EAP_ERP_CS_HMAC_SHA256_128;
787 WPA_PUT_BE16(&ctx[1], erp->rRK_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800788 if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800789 "Re-authentication Integrity Key@ietf.org",
790 ctx, sizeof(ctx), erp->rIK, erp->rRK_len) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800791 wpa_printf(MSG_DEBUG, "EAP: Could not derive rIK for ERP");
792 goto fail;
793 }
794 erp->rIK_len = erp->rRK_len;
795 wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rIK", erp->rIK, erp->rIK_len);
796
797 wpa_printf(MSG_DEBUG, "EAP: Stored ERP keys %s", erp->keyname_nai);
798 dl_list_add(&sm->erp_keys, &erp->list);
799 erp = NULL;
800fail:
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800801 if (ext_emsk)
802 bin_clear_free(ext_emsk, ext_emsk_len);
803 else
804 bin_clear_free(emsk, emsk_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700805 bin_clear_free(ext_session_id, ext_session_id_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800806 bin_clear_free(erp, sizeof(*erp));
807 os_free(realm);
808#endif /* CONFIG_ERP */
809}
810
811
812#ifdef CONFIG_ERP
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800813struct wpabuf * eap_peer_build_erp_reauth_start(struct eap_sm *sm, u8 eap_id)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800814{
815 char *realm;
816 struct eap_erp_key *erp;
817 struct wpabuf *msg;
818 u8 hash[SHA256_MAC_LEN];
819
820 realm = eap_home_realm(sm);
821 if (!realm)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800822 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800823
824 erp = eap_erp_get_key(sm, realm);
825 os_free(realm);
826 realm = NULL;
827 if (!erp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800828 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800829
830 if (erp->next_seq >= 65536)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800831 return NULL; /* SEQ has range of 0..65535 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800832
833 /* TODO: check rRK lifetime expiration */
834
835 wpa_printf(MSG_DEBUG, "EAP: Valid ERP key found %s (SEQ=%u)",
836 erp->keyname_nai, erp->next_seq);
837
Hai Shalomc3565922019-10-28 11:58:20 -0700838 msg = eap_msg_alloc(EAP_VENDOR_IETF,
839 (enum eap_type) EAP_ERP_TYPE_REAUTH,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800840 1 + 2 + 2 + os_strlen(erp->keyname_nai) + 1 + 16,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800841 EAP_CODE_INITIATE, eap_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800842 if (msg == NULL)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800843 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800844
845 wpabuf_put_u8(msg, 0x20); /* Flags: R=0 B=0 L=1 */
846 wpabuf_put_be16(msg, erp->next_seq);
847
848 wpabuf_put_u8(msg, EAP_ERP_TLV_KEYNAME_NAI);
849 wpabuf_put_u8(msg, os_strlen(erp->keyname_nai));
850 wpabuf_put_str(msg, erp->keyname_nai);
851
852 wpabuf_put_u8(msg, EAP_ERP_CS_HMAC_SHA256_128); /* Cryptosuite */
853
854 if (hmac_sha256(erp->rIK, erp->rIK_len,
855 wpabuf_head(msg), wpabuf_len(msg), hash) < 0) {
856 wpabuf_free(msg);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800857 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800858 }
859 wpabuf_put_data(msg, hash, 16);
860
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800861 sm->erp_seq = erp->next_seq;
862 erp->next_seq++;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800863
864 wpa_hexdump_buf(MSG_DEBUG, "ERP: EAP-Initiate/Re-auth", msg);
865
866 return msg;
867}
868
869
870static int eap_peer_erp_reauth_start(struct eap_sm *sm, u8 eap_id)
871{
872 struct wpabuf *msg;
873
874 msg = eap_peer_build_erp_reauth_start(sm, eap_id);
875 if (!msg)
876 return -1;
877
878 wpa_printf(MSG_DEBUG, "EAP: Sending EAP-Initiate/Re-auth");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800879 wpabuf_free(sm->eapRespData);
880 sm->eapRespData = msg;
Hai Shalome21d4e82020-04-29 16:34:06 -0700881 sm->reauthInit = true;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800882 return 0;
883}
884#endif /* CONFIG_ERP */
885
886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887/*
888 * The method processing happens here. The request from the authenticator is
889 * processed, and an appropriate response packet is built.
890 */
891SM_STATE(EAP, METHOD)
892{
893 struct wpabuf *eapReqData;
894 struct eap_method_ret ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800895 int min_len = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896
897 SM_ENTRY(EAP, METHOD);
898 if (sm->m == NULL) {
899 wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected");
900 return;
901 }
902
903 eapReqData = eapol_get_eapReqData(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800904 if (sm->m->vendor == EAP_VENDOR_IETF && sm->m->method == EAP_TYPE_LEAP)
905 min_len = 0; /* LEAP uses EAP-Success without payload */
906 if (!eap_hdr_len_valid(eapReqData, min_len))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700907 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908
909 /*
910 * Get ignore, methodState, decision, allowNotifications, and
911 * eapRespData. RFC 4137 uses three separate method procedure (check,
912 * process, and buildResp) in this state. These have been combined into
913 * a single function call to m->process() in order to optimize EAP
914 * method implementation interface a bit. These procedures are only
915 * used from within this METHOD state, so there is no need to keep
916 * these as separate C functions.
917 *
918 * The RFC 4137 procedures return values as follows:
919 * ignore = m.check(eapReqData)
920 * (methodState, decision, allowNotifications) = m.process(eapReqData)
921 * eapRespData = m.buildResp(reqId)
922 */
923 os_memset(&ret, 0, sizeof(ret));
924 ret.ignore = sm->ignore;
925 ret.methodState = sm->methodState;
926 ret.decision = sm->decision;
927 ret.allowNotifications = sm->allowNotifications;
928 wpabuf_free(sm->eapRespData);
929 sm->eapRespData = NULL;
930 sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret,
931 eapReqData);
932 wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s "
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800933 "methodState=%s decision=%s eapRespData=%p",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 ret.ignore ? "TRUE" : "FALSE",
935 eap_sm_method_state_txt(ret.methodState),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800936 eap_sm_decision_txt(ret.decision),
937 sm->eapRespData);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938
939 sm->ignore = ret.ignore;
940 if (sm->ignore)
941 return;
942 sm->methodState = ret.methodState;
943 sm->decision = ret.decision;
944 sm->allowNotifications = ret.allowNotifications;
945
946 if (sm->m->isKeyAvailable && sm->m->getKey &&
947 sm->m->isKeyAvailable(sm, sm->eap_method_priv)) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700948 eap_sm_free_key(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv,
950 &sm->eapKeyDataLen);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800951 os_free(sm->eapSessionId);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700952 sm->eapSessionId = NULL;
953 if (sm->m->getSessionId) {
954 sm->eapSessionId = sm->m->getSessionId(
955 sm, sm->eap_method_priv,
956 &sm->eapSessionIdLen);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800957 wpa_hexdump(MSG_DEBUG, "EAP: Session-Id",
958 sm->eapSessionId, sm->eapSessionIdLen);
959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 }
961}
962
963
964/*
965 * This state signals the lower layer that a response packet is ready to be
966 * sent.
967 */
968SM_STATE(EAP, SEND_RESPONSE)
969{
970 SM_ENTRY(EAP, SEND_RESPONSE);
971 wpabuf_free(sm->lastRespData);
972 if (sm->eapRespData) {
Hai Shalomc3565922019-10-28 11:58:20 -0700973 if (wpabuf_len(sm->eapRespData) >= 20)
974 sm->num_rounds_short = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 if (sm->workaround)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800976 os_memcpy(sm->last_sha1, sm->req_sha1, 20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 sm->lastId = sm->reqId;
978 sm->lastRespData = wpabuf_dup(sm->eapRespData);
Hai Shalome21d4e82020-04-29 16:34:06 -0700979 eapol_set_bool(sm, EAPOL_eapResp, true);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800980 } else {
981 wpa_printf(MSG_DEBUG, "EAP: No eapRespData available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982 sm->lastRespData = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800983 }
Hai Shalome21d4e82020-04-29 16:34:06 -0700984 eapol_set_bool(sm, EAPOL_eapReq, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
Hai Shalome21d4e82020-04-29 16:34:06 -0700986 sm->reauthInit = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987}
988
989
990/*
991 * This state signals the lower layer that the request was discarded, and no
992 * response packet will be sent at this time.
993 */
994SM_STATE(EAP, DISCARD)
995{
996 SM_ENTRY(EAP, DISCARD);
Hai Shalome21d4e82020-04-29 16:34:06 -0700997 eapol_set_bool(sm, EAPOL_eapReq, false);
998 eapol_set_bool(sm, EAPOL_eapNoResp, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999}
1000
1001
1002/*
1003 * Handles requests for Identity method and builds a response.
1004 */
1005SM_STATE(EAP, IDENTITY)
1006{
1007 const struct wpabuf *eapReqData;
1008
1009 SM_ENTRY(EAP, IDENTITY);
1010 eapReqData = eapol_get_eapReqData(sm);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001011 if (!eap_hdr_len_valid(eapReqData, 1))
1012 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013 eap_sm_processIdentity(sm, eapReqData);
1014 wpabuf_free(sm->eapRespData);
1015 sm->eapRespData = NULL;
1016 sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId, 0);
1017}
1018
1019
1020/*
1021 * Handles requests for Notification method and builds a response.
1022 */
1023SM_STATE(EAP, NOTIFICATION)
1024{
1025 const struct wpabuf *eapReqData;
1026
1027 SM_ENTRY(EAP, NOTIFICATION);
1028 eapReqData = eapol_get_eapReqData(sm);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001029 if (!eap_hdr_len_valid(eapReqData, 1))
1030 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 eap_sm_processNotify(sm, eapReqData);
1032 wpabuf_free(sm->eapRespData);
1033 sm->eapRespData = NULL;
1034 sm->eapRespData = eap_sm_buildNotify(sm->reqId);
1035}
1036
1037
1038/*
1039 * This state retransmits the previous response packet.
1040 */
1041SM_STATE(EAP, RETRANSMIT)
1042{
1043 SM_ENTRY(EAP, RETRANSMIT);
1044 wpabuf_free(sm->eapRespData);
1045 if (sm->lastRespData)
1046 sm->eapRespData = wpabuf_dup(sm->lastRespData);
1047 else
1048 sm->eapRespData = NULL;
1049}
1050
1051
1052/*
1053 * This state is entered in case of a successful completion of authentication
1054 * and state machine waits here until port is disabled or EAP authentication is
1055 * restarted.
1056 */
1057SM_STATE(EAP, SUCCESS)
1058{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001059 struct eap_peer_config *config = eap_get_config(sm);
1060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 SM_ENTRY(EAP, SUCCESS);
1062 if (sm->eapKeyData != NULL)
Hai Shalome21d4e82020-04-29 16:34:06 -07001063 sm->eapKeyAvailable = true;
1064 eapol_set_bool(sm, EAPOL_eapSuccess, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065
1066 /*
1067 * RFC 4137 does not clear eapReq here, but this seems to be required
1068 * to avoid processing the same request twice when state machine is
1069 * initialized.
1070 */
Hai Shalome21d4e82020-04-29 16:34:06 -07001071 eapol_set_bool(sm, EAPOL_eapReq, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072
1073 /*
1074 * RFC 4137 does not set eapNoResp here, but this seems to be required
1075 * to get EAPOL Supplicant backend state machine into SUCCESS state. In
1076 * addition, either eapResp or eapNoResp is required to be set after
1077 * processing the received EAP frame.
1078 */
Hai Shalome21d4e82020-04-29 16:34:06 -07001079 eapol_set_bool(sm, EAPOL_eapNoResp, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080
1081 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
1082 "EAP authentication completed successfully");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001083
Hai Shalomc1a21442022-02-04 13:43:00 -08001084 if (!config || !sm->m) {
1085 /*
1086 * This should not happen under normal conditions, but be more
1087 * careful here since there was an earlier case where
1088 * EAP-Success could end up getting delivered to the state
1089 * machine for processing after the state had been cleaned with
1090 * a call to eap_invalidate_cached_session() (and also
1091 * eapol_sm_notify_config() having been used to clear EAP
1092 * configuration in the EAPOL state machine).
1093 */
1094 wpa_printf(MSG_DEBUG,
1095 "EAP: State machine not configured - cannot initialize ERP");
1096 return;
1097 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001098 if (config->erp && sm->m->get_emsk && sm->eapSessionId &&
1099 sm->m->isKeyAvailable &&
1100 sm->m->isKeyAvailable(sm, sm->eap_method_priv))
1101 eap_peer_erp_init(sm, NULL, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102}
1103
1104
1105/*
1106 * This state is entered in case of a failure and state machine waits here
1107 * until port is disabled or EAP authentication is restarted.
1108 */
1109SM_STATE(EAP, FAILURE)
1110{
1111 SM_ENTRY(EAP, FAILURE);
Hai Shalome21d4e82020-04-29 16:34:06 -07001112 eapol_set_bool(sm, EAPOL_eapFail, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113
1114 /*
1115 * RFC 4137 does not clear eapReq here, but this seems to be required
1116 * to avoid processing the same request twice when state machine is
1117 * initialized.
1118 */
Hai Shalome21d4e82020-04-29 16:34:06 -07001119 eapol_set_bool(sm, EAPOL_eapReq, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120
1121 /*
1122 * RFC 4137 does not set eapNoResp here. However, either eapResp or
1123 * eapNoResp is required to be set after processing the received EAP
1124 * frame.
1125 */
Hai Shalome21d4e82020-04-29 16:34:06 -07001126 eapol_set_bool(sm, EAPOL_eapNoResp, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127
1128 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
1129 "EAP authentication failed");
1130
1131 sm->prev_failure = 1;
1132}
1133
1134
1135static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId)
1136{
1137 /*
1138 * At least Microsoft IAS and Meetinghouse Aegis seem to be sending
1139 * EAP-Success/Failure with lastId + 1 even though RFC 3748 and
1140 * RFC 4137 require that reqId == lastId. In addition, it looks like
1141 * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
1142 *
1143 * Accept this kind of Id if EAP workarounds are enabled. These are
1144 * unauthenticated plaintext messages, so this should have minimal
1145 * security implications (bit easier to fake EAP-Success/Failure).
1146 */
1147 if (sm->workaround && (reqId == ((lastId + 1) & 0xff) ||
1148 reqId == ((lastId + 2) & 0xff))) {
1149 wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected "
1150 "identifier field in EAP Success: "
1151 "reqId=%d lastId=%d (these are supposed to be "
1152 "same)", reqId, lastId);
1153 return 1;
1154 }
1155 wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d "
1156 "lastId=%d", reqId, lastId);
1157 return 0;
1158}
1159
1160
1161/*
1162 * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
1163 */
1164
1165static void eap_peer_sm_step_idle(struct eap_sm *sm)
1166{
1167 /*
1168 * The first three transitions are from RFC 4137. The last two are
1169 * local additions to handle special cases with LEAP and PEAP server
1170 * not sending EAP-Success in some cases.
1171 */
1172 if (eapol_get_bool(sm, EAPOL_eapReq))
1173 SM_ENTER(EAP, RECEIVED);
1174 else if ((eapol_get_bool(sm, EAPOL_altAccept) &&
1175 sm->decision != DECISION_FAIL) ||
1176 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
1177 sm->decision == DECISION_UNCOND_SUCC))
1178 SM_ENTER(EAP, SUCCESS);
1179 else if (eapol_get_bool(sm, EAPOL_altReject) ||
1180 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
1181 sm->decision != DECISION_UNCOND_SUCC) ||
1182 (eapol_get_bool(sm, EAPOL_altAccept) &&
1183 sm->methodState != METHOD_CONT &&
1184 sm->decision == DECISION_FAIL))
1185 SM_ENTER(EAP, FAILURE);
1186 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
1187 sm->leap_done && sm->decision != DECISION_FAIL &&
1188 sm->methodState == METHOD_DONE)
1189 SM_ENTER(EAP, SUCCESS);
1190 else if (sm->selectedMethod == EAP_TYPE_PEAP &&
1191 sm->peap_done && sm->decision != DECISION_FAIL &&
1192 sm->methodState == METHOD_DONE)
1193 SM_ENTER(EAP, SUCCESS);
1194}
1195
1196
1197static int eap_peer_req_is_duplicate(struct eap_sm *sm)
1198{
1199 int duplicate;
1200
1201 duplicate = (sm->reqId == sm->lastId) && sm->rxReq;
1202 if (sm->workaround && duplicate &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001203 os_memcmp(sm->req_sha1, sm->last_sha1, 20) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 /*
1205 * RFC 4137 uses (reqId == lastId) as the only verification for
1206 * duplicate EAP requests. However, this misses cases where the
1207 * AS is incorrectly using the same id again; and
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001208 * unfortunately, such implementations exist. Use SHA1 hash as
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209 * an extra verification for the packets being duplicate to
1210 * workaround these issues.
1211 */
1212 wpa_printf(MSG_DEBUG, "EAP: AS used the same Id again, but "
1213 "EAP packets were not identical");
1214 wpa_printf(MSG_DEBUG, "EAP: workaround - assume this is not a "
1215 "duplicate packet");
1216 duplicate = 0;
1217 }
1218
1219 return duplicate;
1220}
1221
1222
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001223static int eap_peer_sm_allow_canned(struct eap_sm *sm)
1224{
1225 struct eap_peer_config *config = eap_get_config(sm);
1226
1227 return config && config->phase1 &&
1228 os_strstr(config->phase1, "allow_canned_success=1");
1229}
1230
1231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232static void eap_peer_sm_step_received(struct eap_sm *sm)
1233{
1234 int duplicate = eap_peer_req_is_duplicate(sm);
1235
1236 /*
1237 * Two special cases below for LEAP are local additions to work around
1238 * odd LEAP behavior (EAP-Success in the middle of authentication and
1239 * then swapped roles). Other transitions are based on RFC 4137.
1240 */
1241 if (sm->rxSuccess && sm->decision != DECISION_FAIL &&
1242 (sm->reqId == sm->lastId ||
1243 eap_success_workaround(sm, sm->reqId, sm->lastId)))
1244 SM_ENTER(EAP, SUCCESS);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001245 else if (sm->workaround && sm->lastId == -1 && sm->rxSuccess &&
1246 !sm->rxFailure && !sm->rxReq && eap_peer_sm_allow_canned(sm))
1247 SM_ENTER(EAP, SUCCESS); /* EAP-Success prior any EAP method */
1248 else if (sm->workaround && sm->lastId == -1 && sm->rxFailure &&
1249 !sm->rxReq && sm->methodState != METHOD_CONT &&
1250 eap_peer_sm_allow_canned(sm))
1251 SM_ENTER(EAP, FAILURE); /* EAP-Failure prior any EAP method */
1252 else if (sm->workaround && sm->rxSuccess && !sm->rxFailure &&
1253 !sm->rxReq && sm->methodState != METHOD_CONT &&
1254 eap_peer_sm_allow_canned(sm))
1255 SM_ENTER(EAP, SUCCESS); /* EAP-Success after Identity */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 else if (sm->methodState != METHOD_CONT &&
1257 ((sm->rxFailure &&
1258 sm->decision != DECISION_UNCOND_SUCC) ||
1259 (sm->rxSuccess && sm->decision == DECISION_FAIL &&
1260 (sm->selectedMethod != EAP_TYPE_LEAP ||
1261 sm->methodState != METHOD_MAY_CONT))) &&
1262 (sm->reqId == sm->lastId ||
1263 eap_success_workaround(sm, sm->reqId, sm->lastId)))
1264 SM_ENTER(EAP, FAILURE);
1265 else if (sm->rxReq && duplicate)
1266 SM_ENTER(EAP, RETRANSMIT);
1267 else if (sm->rxReq && !duplicate &&
1268 sm->reqMethod == EAP_TYPE_NOTIFICATION &&
1269 sm->allowNotifications)
1270 SM_ENTER(EAP, NOTIFICATION);
1271 else if (sm->rxReq && !duplicate &&
1272 sm->selectedMethod == EAP_TYPE_NONE &&
1273 sm->reqMethod == EAP_TYPE_IDENTITY)
1274 SM_ENTER(EAP, IDENTITY);
1275 else if (sm->rxReq && !duplicate &&
1276 sm->selectedMethod == EAP_TYPE_NONE &&
1277 sm->reqMethod != EAP_TYPE_IDENTITY &&
1278 sm->reqMethod != EAP_TYPE_NOTIFICATION)
1279 SM_ENTER(EAP, GET_METHOD);
1280 else if (sm->rxReq && !duplicate &&
1281 sm->reqMethod == sm->selectedMethod &&
1282 sm->methodState != METHOD_DONE)
1283 SM_ENTER(EAP, METHOD);
1284 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
1285 (sm->rxSuccess || sm->rxResp))
1286 SM_ENTER(EAP, METHOD);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001287 else if (sm->reauthInit)
1288 SM_ENTER(EAP, SEND_RESPONSE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 else
1290 SM_ENTER(EAP, DISCARD);
1291}
1292
1293
1294static void eap_peer_sm_step_local(struct eap_sm *sm)
1295{
1296 switch (sm->EAP_state) {
1297 case EAP_INITIALIZE:
1298 SM_ENTER(EAP, IDLE);
1299 break;
1300 case EAP_DISABLED:
1301 if (eapol_get_bool(sm, EAPOL_portEnabled) &&
1302 !sm->force_disabled)
1303 SM_ENTER(EAP, INITIALIZE);
1304 break;
1305 case EAP_IDLE:
1306 eap_peer_sm_step_idle(sm);
1307 break;
1308 case EAP_RECEIVED:
1309 eap_peer_sm_step_received(sm);
1310 break;
1311 case EAP_GET_METHOD:
1312 if (sm->selectedMethod == sm->reqMethod)
1313 SM_ENTER(EAP, METHOD);
1314 else
1315 SM_ENTER(EAP, SEND_RESPONSE);
1316 break;
1317 case EAP_METHOD:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001318 /*
1319 * Note: RFC 4137 uses methodState == DONE && decision == FAIL
1320 * as the condition. eapRespData == NULL here is used to allow
1321 * final EAP method response to be sent without having to change
1322 * all methods to either use methodState MAY_CONT or leaving
1323 * decision to something else than FAIL in cases where the only
1324 * expected response is EAP-Failure.
1325 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326 if (sm->ignore)
1327 SM_ENTER(EAP, DISCARD);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001328 else if (sm->methodState == METHOD_DONE &&
1329 sm->decision == DECISION_FAIL && !sm->eapRespData)
1330 SM_ENTER(EAP, FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 else
1332 SM_ENTER(EAP, SEND_RESPONSE);
1333 break;
1334 case EAP_SEND_RESPONSE:
1335 SM_ENTER(EAP, IDLE);
1336 break;
1337 case EAP_DISCARD:
1338 SM_ENTER(EAP, IDLE);
1339 break;
1340 case EAP_IDENTITY:
1341 SM_ENTER(EAP, SEND_RESPONSE);
1342 break;
1343 case EAP_NOTIFICATION:
1344 SM_ENTER(EAP, SEND_RESPONSE);
1345 break;
1346 case EAP_RETRANSMIT:
1347 SM_ENTER(EAP, SEND_RESPONSE);
1348 break;
1349 case EAP_SUCCESS:
1350 break;
1351 case EAP_FAILURE:
1352 break;
1353 }
1354}
1355
1356
1357SM_STEP(EAP)
1358{
1359 /* Global transitions */
1360 if (eapol_get_bool(sm, EAPOL_eapRestart) &&
1361 eapol_get_bool(sm, EAPOL_portEnabled))
1362 SM_ENTER_GLOBAL(EAP, INITIALIZE);
1363 else if (!eapol_get_bool(sm, EAPOL_portEnabled) || sm->force_disabled)
1364 SM_ENTER_GLOBAL(EAP, DISABLED);
1365 else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
1366 /* RFC 4137 does not place any limit on number of EAP messages
1367 * in an authentication session. However, some error cases have
1368 * ended up in a state were EAP messages were sent between the
1369 * peer and server in a loop (e.g., TLS ACK frame in both
1370 * direction). Since this is quite undesired outcome, limit the
1371 * total number of EAP round-trips and abort authentication if
1372 * this limit is exceeded.
1373 */
1374 if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
1375 wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: more than %d "
1376 "authentication rounds - abort",
1377 EAP_MAX_AUTH_ROUNDS);
1378 sm->num_rounds++;
1379 SM_ENTER_GLOBAL(EAP, FAILURE);
1380 }
Hai Shalomc3565922019-10-28 11:58:20 -07001381 } else if (sm->num_rounds_short > EAP_MAX_AUTH_ROUNDS_SHORT) {
1382 if (sm->num_rounds_short == EAP_MAX_AUTH_ROUNDS_SHORT + 1) {
1383 wpa_msg(sm->msg_ctx, MSG_INFO,
1384 "EAP: more than %d authentication rounds (short) - abort",
1385 EAP_MAX_AUTH_ROUNDS_SHORT);
1386 sm->num_rounds_short++;
1387 SM_ENTER_GLOBAL(EAP, FAILURE);
1388 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389 } else {
1390 /* Local transitions */
1391 eap_peer_sm_step_local(sm);
1392 }
1393}
1394
1395
Hai Shalome21d4e82020-04-29 16:34:06 -07001396static bool eap_sm_allowMethod(struct eap_sm *sm, int vendor,
1397 enum eap_type method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398{
1399 if (!eap_allowed_method(sm, vendor, method)) {
1400 wpa_printf(MSG_DEBUG, "EAP: configuration does not allow: "
1401 "vendor %u method %u", vendor, method);
Hai Shalome21d4e82020-04-29 16:34:06 -07001402 return false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 }
1404 if (eap_peer_get_eap_method(vendor, method))
Hai Shalome21d4e82020-04-29 16:34:06 -07001405 return true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 wpa_printf(MSG_DEBUG, "EAP: not included in build: "
1407 "vendor %u method %u", vendor, method);
Hai Shalome21d4e82020-04-29 16:34:06 -07001408 return false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409}
1410
1411
1412static struct wpabuf * eap_sm_build_expanded_nak(
1413 struct eap_sm *sm, int id, const struct eap_method *methods,
1414 size_t count)
1415{
1416 struct wpabuf *resp;
1417 int found = 0;
1418 const struct eap_method *m;
1419
1420 wpa_printf(MSG_DEBUG, "EAP: Building expanded EAP-Nak");
1421
1422 /* RFC 3748 - 5.3.2: Expanded Nak */
1423 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_EXPANDED,
1424 8 + 8 * (count + 1), EAP_CODE_RESPONSE, id);
1425 if (resp == NULL)
1426 return NULL;
1427
1428 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
1429 wpabuf_put_be32(resp, EAP_TYPE_NAK);
1430
1431 for (m = methods; m; m = m->next) {
1432 if (sm->reqVendor == m->vendor &&
1433 sm->reqVendorMethod == m->method)
1434 continue; /* do not allow the current method again */
1435 if (eap_allowed_method(sm, m->vendor, m->method)) {
1436 wpa_printf(MSG_DEBUG, "EAP: allowed type: "
1437 "vendor=%u method=%u",
1438 m->vendor, m->method);
1439 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
1440 wpabuf_put_be24(resp, m->vendor);
1441 wpabuf_put_be32(resp, m->method);
1442
1443 found++;
1444 }
1445 }
1446 if (!found) {
1447 wpa_printf(MSG_DEBUG, "EAP: no more allowed methods");
1448 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
1449 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
1450 wpabuf_put_be32(resp, EAP_TYPE_NONE);
1451 }
1452
1453 eap_update_len(resp);
1454
1455 return resp;
1456}
1457
1458
1459static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id)
1460{
1461 struct wpabuf *resp;
1462 u8 *start;
1463 int found = 0, expanded_found = 0;
1464 size_t count;
1465 const struct eap_method *methods, *m;
1466
1467 wpa_printf(MSG_DEBUG, "EAP: Building EAP-Nak (requested type %u "
1468 "vendor=%u method=%u not allowed)", sm->reqMethod,
1469 sm->reqVendor, sm->reqVendorMethod);
1470 methods = eap_peer_get_methods(&count);
1471 if (methods == NULL)
1472 return NULL;
1473 if (sm->reqMethod == EAP_TYPE_EXPANDED)
1474 return eap_sm_build_expanded_nak(sm, id, methods, count);
1475
1476 /* RFC 3748 - 5.3.1: Legacy Nak */
1477 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK,
1478 sizeof(struct eap_hdr) + 1 + count + 1,
1479 EAP_CODE_RESPONSE, id);
1480 if (resp == NULL)
1481 return NULL;
1482
1483 start = wpabuf_put(resp, 0);
1484 for (m = methods; m; m = m->next) {
1485 if (m->vendor == EAP_VENDOR_IETF && m->method == sm->reqMethod)
1486 continue; /* do not allow the current method again */
1487 if (eap_allowed_method(sm, m->vendor, m->method)) {
1488 if (m->vendor != EAP_VENDOR_IETF) {
1489 if (expanded_found)
1490 continue;
1491 expanded_found = 1;
1492 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
1493 } else
1494 wpabuf_put_u8(resp, m->method);
1495 found++;
1496 }
1497 }
1498 if (!found)
1499 wpabuf_put_u8(resp, EAP_TYPE_NONE);
1500 wpa_hexdump(MSG_DEBUG, "EAP: allowed methods", start, found);
1501
1502 eap_update_len(resp);
1503
1504 return resp;
1505}
1506
1507
1508static void eap_sm_processIdentity(struct eap_sm *sm, const struct wpabuf *req)
1509{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001510 const u8 *pos;
1511 size_t msg_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512
1513 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
1514 "EAP authentication started");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001515 eap_notify_status(sm, "started", "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001517 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, req,
1518 &msg_len);
1519 if (pos == NULL)
1520 return;
1521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001522 /*
1523 * RFC 3748 - 5.1: Identity
1524 * Data field may contain a displayable message in UTF-8. If this
1525 * includes NUL-character, only the data before that should be
1526 * displayed. Some EAP implementasitons may piggy-back additional
1527 * options after the NUL.
1528 */
1529 /* TODO: could save displayable message so that it can be shown to the
1530 * user in case of interaction is required */
1531 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Identity data",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001532 pos, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533}
1534
1535
1536#ifdef PCSC_FUNCS
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001537
1538/*
1539 * Rules for figuring out MNC length based on IMSI for SIM cards that do not
1540 * include MNC length field.
1541 */
1542static int mnc_len_from_imsi(const char *imsi)
1543{
1544 char mcc_str[4];
1545 unsigned int mcc;
1546
1547 os_memcpy(mcc_str, imsi, 3);
1548 mcc_str[3] = '\0';
1549 mcc = atoi(mcc_str);
1550
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001551 if (mcc == 228)
1552 return 2; /* Networks in Switzerland use 2-digit MNC */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001553 if (mcc == 244)
1554 return 2; /* Networks in Finland use 2-digit MNC */
1555
1556 return -1;
1557}
1558
1559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560static int eap_sm_imsi_identity(struct eap_sm *sm,
1561 struct eap_peer_config *conf)
1562{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001563 enum { EAP_SM_SIM, EAP_SM_AKA, EAP_SM_AKA_PRIME } method = EAP_SM_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 char imsi[100];
1565 size_t imsi_len;
1566 struct eap_method_type *m = conf->eap_methods;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001567 int i, mnc_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568
1569 imsi_len = sizeof(imsi);
1570 if (scard_get_imsi(sm->scard_ctx, imsi, &imsi_len)) {
1571 wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM");
1572 return -1;
1573 }
1574
1575 wpa_hexdump_ascii(MSG_DEBUG, "IMSI", (u8 *) imsi, imsi_len);
1576
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001577 if (imsi_len < 7) {
1578 wpa_printf(MSG_WARNING, "Too short IMSI for SIM identity");
1579 return -1;
1580 }
1581
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001582 /* MNC (2 or 3 digits) */
1583 mnc_len = scard_get_mnc_len(sm->scard_ctx);
1584 if (mnc_len < 0)
1585 mnc_len = mnc_len_from_imsi(imsi);
1586 if (mnc_len < 0) {
1587 wpa_printf(MSG_INFO, "Failed to get MNC length from (U)SIM "
1588 "assuming 3");
1589 mnc_len = 3;
1590 }
1591
1592 if (eap_sm_append_3gpp_realm(sm, imsi, sizeof(imsi), &imsi_len,
1593 mnc_len) < 0) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001594 wpa_printf(MSG_WARNING, "Could not add realm to SIM identity");
1595 return -1;
1596 }
1597 wpa_hexdump_ascii(MSG_DEBUG, "IMSI + realm", (u8 *) imsi, imsi_len);
1598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599 for (i = 0; m && (m[i].vendor != EAP_VENDOR_IETF ||
1600 m[i].method != EAP_TYPE_NONE); i++) {
1601 if (m[i].vendor == EAP_VENDOR_IETF &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07001602 m[i].method == EAP_TYPE_AKA_PRIME) {
1603 method = EAP_SM_AKA_PRIME;
1604 break;
1605 }
1606
1607 if (m[i].vendor == EAP_VENDOR_IETF &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 m[i].method == EAP_TYPE_AKA) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001609 method = EAP_SM_AKA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 break;
1611 }
1612 }
1613
1614 os_free(conf->identity);
1615 conf->identity = os_malloc(1 + imsi_len);
1616 if (conf->identity == NULL) {
1617 wpa_printf(MSG_WARNING, "Failed to allocate buffer for "
1618 "IMSI-based identity");
1619 return -1;
1620 }
1621
Dmitry Shmidt04949592012-07-19 12:16:46 -07001622 switch (method) {
1623 case EAP_SM_SIM:
1624 conf->identity[0] = '1';
1625 break;
1626 case EAP_SM_AKA:
1627 conf->identity[0] = '0';
1628 break;
1629 case EAP_SM_AKA_PRIME:
1630 conf->identity[0] = '6';
1631 break;
1632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 os_memcpy(conf->identity + 1, imsi, imsi_len);
1634 conf->identity_len = 1 + imsi_len;
1635
1636 return 0;
1637}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639
1640static int eap_sm_set_scard_pin(struct eap_sm *sm,
1641 struct eap_peer_config *conf)
1642{
Hai Shalomc3565922019-10-28 11:58:20 -07001643 if (scard_set_pin(sm->scard_ctx, conf->cert.pin)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 /*
1645 * Make sure the same PIN is not tried again in order to avoid
1646 * blocking SIM.
1647 */
Hai Shalomc3565922019-10-28 11:58:20 -07001648 os_free(conf->cert.pin);
1649 conf->cert.pin = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650
1651 wpa_printf(MSG_WARNING, "PIN validation failed");
1652 eap_sm_request_pin(sm);
1653 return -1;
1654 }
1655 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001656}
1657
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659static int eap_sm_get_scard_identity(struct eap_sm *sm,
1660 struct eap_peer_config *conf)
1661{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 if (eap_sm_set_scard_pin(sm, conf))
1663 return -1;
1664
1665 return eap_sm_imsi_identity(sm, conf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666}
1667
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001668#endif /* PCSC_FUNCS */
1669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670
1671/**
1672 * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
1673 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1674 * @id: EAP identifier for the packet
1675 * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
1676 * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
1677 * failure
1678 *
1679 * This function allocates and builds an EAP-Identity/Response packet for the
1680 * current network. The caller is responsible for freeing the returned data.
1681 */
1682struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted)
1683{
1684 struct eap_peer_config *config = eap_get_config(sm);
1685 struct wpabuf *resp;
1686 const u8 *identity;
1687 size_t identity_len;
Sunil Ravia04bd252022-05-02 22:54:18 -07001688 struct wpabuf *privacy_identity = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689
1690 if (config == NULL) {
1691 wpa_printf(MSG_WARNING, "EAP: buildIdentity: configuration "
1692 "was not available");
1693 return NULL;
1694 }
1695
1696 if (sm->m && sm->m->get_identity &&
1697 (identity = sm->m->get_identity(sm, sm->eap_method_priv,
1698 &identity_len)) != NULL) {
1699 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth "
1700 "identity", identity, identity_len);
1701 } else if (!encrypted && config->anonymous_identity) {
1702 identity = config->anonymous_identity;
1703 identity_len = config->anonymous_identity_len;
1704 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity",
1705 identity, identity_len);
Hai Shalomc3565922019-10-28 11:58:20 -07001706 } else if (sm->use_machine_cred) {
1707 identity = config->machine_identity;
1708 identity_len = config->machine_identity_len;
1709 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using machine identity",
1710 identity, identity_len);
Sunil8cd6f4d2022-06-28 18:40:46 +00001711 } else if (config->imsi_privacy_cert && config->identity &&
Sunil Ravia04bd252022-05-02 22:54:18 -07001712 config->identity_len > 0) {
1713 const u8 *pos = config->identity;
1714 const u8 *end = config->identity + config->identity_len;
1715
1716 privacy_identity = wpabuf_alloc(9 + config->identity_len);
1717 if (!privacy_identity)
1718 return NULL;
1719
1720 /* Include method prefix */
1721 if (*pos == '0' || *pos == '1' || *pos == '6')
1722 wpabuf_put_u8(privacy_identity, *pos);
1723 wpabuf_put_str(privacy_identity, "anonymous");
1724
1725 /* Include realm */
1726 while (pos < end && *pos != '@')
1727 pos++;
1728 wpabuf_put_data(privacy_identity, pos, end - pos);
1729
1730 identity = wpabuf_head(privacy_identity);
1731 identity_len = wpabuf_len(privacy_identity);
1732 wpa_hexdump_ascii(MSG_DEBUG,
1733 "EAP: using IMSI privacy anonymous identity",
1734 identity, identity_len);
Steven Liu9138d432022-11-23 22:29:05 +00001735 } else if (config->strict_conservative_peer_mode) {
1736 wpa_printf(MSG_DEBUG, "EAP: never use real identity in conservative peer mode.");
1737 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 } else {
1739 identity = config->identity;
1740 identity_len = config->identity_len;
1741 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity",
1742 identity, identity_len);
1743 }
1744
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001745 if (config->pcsc) {
1746#ifdef PCSC_FUNCS
1747 if (!identity) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 if (eap_sm_get_scard_identity(sm, config) < 0)
1749 return NULL;
1750 identity = config->identity;
1751 identity_len = config->identity_len;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001752 wpa_hexdump_ascii(MSG_DEBUG,
1753 "permanent identity from IMSI",
1754 identity, identity_len);
1755 } else if (eap_sm_set_scard_pin(sm, config) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 return NULL;
1757 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001758#else /* PCSC_FUNCS */
1759 return NULL;
1760#endif /* PCSC_FUNCS */
1761 } else if (!identity) {
1762 wpa_printf(MSG_WARNING,
1763 "EAP: buildIdentity: identity configuration was not available");
1764 eap_sm_request_identity(sm);
1765 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 }
1767
1768 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, identity_len,
1769 EAP_CODE_RESPONSE, id);
1770 if (resp == NULL)
1771 return NULL;
1772
1773 wpabuf_put_data(resp, identity, identity_len);
Sunil Ravieb83e2a2024-06-28 17:34:56 +00001774 wpabuf_free(privacy_identity);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001776 os_free(sm->identity);
1777 sm->identity = os_memdup(identity, identity_len);
1778 sm->identity_len = identity_len;
1779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 return resp;
1781}
1782
1783
1784static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req)
1785{
1786 const u8 *pos;
1787 char *msg;
1788 size_t i, msg_len;
1789
1790 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, req,
1791 &msg_len);
1792 if (pos == NULL)
1793 return;
1794 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Notification data",
1795 pos, msg_len);
1796
1797 msg = os_malloc(msg_len + 1);
1798 if (msg == NULL)
1799 return;
1800 for (i = 0; i < msg_len; i++)
1801 msg[i] = isprint(pos[i]) ? (char) pos[i] : '_';
1802 msg[msg_len] = '\0';
1803 wpa_msg(sm->msg_ctx, MSG_INFO, "%s%s",
1804 WPA_EVENT_EAP_NOTIFICATION, msg);
1805 os_free(msg);
1806}
1807
1808
1809static struct wpabuf * eap_sm_buildNotify(int id)
1810{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811 wpa_printf(MSG_DEBUG, "EAP: Generating EAP-Response Notification");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001812 return eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, 0,
1813 EAP_CODE_RESPONSE, id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001814}
1815
1816
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001817static void eap_peer_initiate(struct eap_sm *sm, const struct eap_hdr *hdr,
1818 size_t len)
1819{
1820#ifdef CONFIG_ERP
1821 const u8 *pos = (const u8 *) (hdr + 1);
1822 const u8 *end = ((const u8 *) hdr) + len;
1823 struct erp_tlvs parse;
1824
1825 if (len < sizeof(*hdr) + 1) {
1826 wpa_printf(MSG_DEBUG, "EAP: Ignored too short EAP-Initiate");
1827 return;
1828 }
1829
1830 if (*pos != EAP_ERP_TYPE_REAUTH_START) {
1831 wpa_printf(MSG_DEBUG,
1832 "EAP: Ignored unexpected EAP-Initiate Type=%u",
1833 *pos);
1834 return;
1835 }
1836
1837 pos++;
1838 if (pos >= end) {
1839 wpa_printf(MSG_DEBUG,
1840 "EAP: Too short EAP-Initiate/Re-auth-Start");
1841 return;
1842 }
1843 pos++; /* Reserved */
1844 wpa_hexdump(MSG_DEBUG, "EAP: EAP-Initiate/Re-auth-Start TVs/TLVs",
1845 pos, end - pos);
1846
1847 if (erp_parse_tlvs(pos, end, &parse, 0) < 0)
1848 goto invalid;
1849
1850 if (parse.domain) {
1851 wpa_hexdump_ascii(MSG_DEBUG,
1852 "EAP: EAP-Initiate/Re-auth-Start - Domain name",
1853 parse.domain, parse.domain_len);
1854 /* TODO: Derivation of domain specific keys for local ER */
1855 }
1856
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001857 if (eap_peer_erp_reauth_start(sm, hdr->identifier) == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001858 return;
1859
1860invalid:
1861#endif /* CONFIG_ERP */
1862 wpa_printf(MSG_DEBUG,
1863 "EAP: EAP-Initiate/Re-auth-Start - No suitable ERP keys available - try to start full EAP authentication");
Hai Shalome21d4e82020-04-29 16:34:06 -07001864 eapol_set_bool(sm, EAPOL_eapTriggerStart, true);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001865}
1866
1867
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001868void eap_peer_finish(struct eap_sm *sm, const struct eap_hdr *hdr, size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001869{
1870#ifdef CONFIG_ERP
1871 const u8 *pos = (const u8 *) (hdr + 1);
1872 const u8 *end = ((const u8 *) hdr) + len;
1873 const u8 *start;
1874 struct erp_tlvs parse;
1875 u8 flags;
1876 u16 seq;
1877 u8 hash[SHA256_MAC_LEN];
1878 size_t hash_len;
1879 struct eap_erp_key *erp;
1880 int max_len;
1881 char nai[254];
1882 u8 seed[4];
1883 int auth_tag_ok = 0;
1884
1885 if (len < sizeof(*hdr) + 1) {
1886 wpa_printf(MSG_DEBUG, "EAP: Ignored too short EAP-Finish");
1887 return;
1888 }
1889
1890 if (*pos != EAP_ERP_TYPE_REAUTH) {
1891 wpa_printf(MSG_DEBUG,
1892 "EAP: Ignored unexpected EAP-Finish Type=%u", *pos);
1893 return;
1894 }
1895
1896 if (len < sizeof(*hdr) + 4) {
1897 wpa_printf(MSG_DEBUG,
1898 "EAP: Ignored too short EAP-Finish/Re-auth");
1899 return;
1900 }
1901
1902 pos++;
1903 flags = *pos++;
1904 seq = WPA_GET_BE16(pos);
1905 pos += 2;
1906 wpa_printf(MSG_DEBUG, "EAP: Flags=0x%x SEQ=%u", flags, seq);
1907
1908 if (seq != sm->erp_seq) {
1909 wpa_printf(MSG_DEBUG,
1910 "EAP: Unexpected EAP-Finish/Re-auth SEQ=%u", seq);
1911 return;
1912 }
1913
1914 /*
1915 * Parse TVs/TLVs. Since we do not yet know the length of the
1916 * Authentication Tag, stop parsing if an unknown TV/TLV is seen and
1917 * just try to find the keyName-NAI first so that we can check the
1918 * Authentication Tag.
1919 */
1920 if (erp_parse_tlvs(pos, end, &parse, 1) < 0)
1921 return;
1922
1923 if (!parse.keyname) {
1924 wpa_printf(MSG_DEBUG,
1925 "EAP: No keyName-NAI in EAP-Finish/Re-auth Packet");
1926 return;
1927 }
1928
1929 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Finish/Re-auth - keyName-NAI",
1930 parse.keyname, parse.keyname_len);
1931 if (parse.keyname_len > 253) {
1932 wpa_printf(MSG_DEBUG,
1933 "EAP: Too long keyName-NAI in EAP-Finish/Re-auth");
1934 return;
1935 }
1936 os_memcpy(nai, parse.keyname, parse.keyname_len);
1937 nai[parse.keyname_len] = '\0';
1938
1939 erp = eap_erp_get_key_nai(sm, nai);
1940 if (!erp) {
1941 wpa_printf(MSG_DEBUG, "EAP: No matching ERP key found for %s",
1942 nai);
1943 return;
1944 }
1945
1946 /* Is there enough room for Cryptosuite and Authentication Tag? */
1947 start = parse.keyname + parse.keyname_len;
1948 max_len = end - start;
1949 hash_len = 16;
1950 if (max_len < 1 + (int) hash_len) {
1951 wpa_printf(MSG_DEBUG,
1952 "EAP: Not enough room for Authentication Tag");
1953 if (flags & 0x80)
1954 goto no_auth_tag;
1955 return;
1956 }
1957 if (end[-17] != EAP_ERP_CS_HMAC_SHA256_128) {
1958 wpa_printf(MSG_DEBUG, "EAP: Different Cryptosuite used");
1959 if (flags & 0x80)
1960 goto no_auth_tag;
1961 return;
1962 }
1963
1964 if (hmac_sha256(erp->rIK, erp->rIK_len, (const u8 *) hdr,
1965 end - ((const u8 *) hdr) - hash_len, hash) < 0)
1966 return;
1967 if (os_memcmp(end - hash_len, hash, hash_len) != 0) {
1968 wpa_printf(MSG_DEBUG,
1969 "EAP: Authentication Tag mismatch");
1970 return;
1971 }
1972 auth_tag_ok = 1;
1973 end -= 1 + hash_len;
1974
1975no_auth_tag:
1976 /*
1977 * Parse TVs/TLVs again now that we know the exact part of the buffer
1978 * that contains them.
1979 */
1980 wpa_hexdump(MSG_DEBUG, "EAP: EAP-Finish/Re-Auth TVs/TLVs",
1981 pos, end - pos);
1982 if (erp_parse_tlvs(pos, end, &parse, 0) < 0)
1983 return;
1984
1985 if (flags & 0x80 || !auth_tag_ok) {
1986 wpa_printf(MSG_DEBUG,
1987 "EAP: EAP-Finish/Re-auth indicated failure");
Hai Shalome21d4e82020-04-29 16:34:06 -07001988 eapol_set_bool(sm, EAPOL_eapFail, true);
1989 eapol_set_bool(sm, EAPOL_eapReq, false);
1990 eapol_set_bool(sm, EAPOL_eapNoResp, true);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001991 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
1992 "EAP authentication failed");
1993 sm->prev_failure = 1;
1994 wpa_printf(MSG_DEBUG,
1995 "EAP: Drop ERP key to try full authentication on next attempt");
1996 eap_peer_erp_free_key(erp);
1997 return;
1998 }
1999
2000 eap_sm_free_key(sm);
2001 sm->eapKeyDataLen = 0;
2002 sm->eapKeyData = os_malloc(erp->rRK_len);
2003 if (!sm->eapKeyData)
2004 return;
2005 sm->eapKeyDataLen = erp->rRK_len;
2006
2007 WPA_PUT_BE16(seed, seq);
2008 WPA_PUT_BE16(&seed[2], erp->rRK_len);
2009 if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
2010 "Re-authentication Master Session Key@ietf.org",
2011 seed, sizeof(seed),
2012 sm->eapKeyData, erp->rRK_len) < 0) {
2013 wpa_printf(MSG_DEBUG, "EAP: Could not derive rMSK for ERP");
2014 eap_sm_free_key(sm);
2015 return;
2016 }
2017 wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rMSK",
2018 sm->eapKeyData, sm->eapKeyDataLen);
Hai Shalome21d4e82020-04-29 16:34:06 -07002019 sm->eapKeyAvailable = true;
2020 eapol_set_bool(sm, EAPOL_eapSuccess, true);
2021 eapol_set_bool(sm, EAPOL_eapReq, false);
2022 eapol_set_bool(sm, EAPOL_eapNoResp, true);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002023 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
2024 "EAP re-authentication completed successfully");
2025#endif /* CONFIG_ERP */
2026}
2027
2028
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req)
2030{
2031 const struct eap_hdr *hdr;
2032 size_t plen;
2033 const u8 *pos;
2034
Hai Shalome21d4e82020-04-29 16:34:06 -07002035 sm->rxReq = sm->rxResp = sm->rxSuccess = sm->rxFailure = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 sm->reqId = 0;
2037 sm->reqMethod = EAP_TYPE_NONE;
2038 sm->reqVendor = EAP_VENDOR_IETF;
2039 sm->reqVendorMethod = EAP_TYPE_NONE;
2040
2041 if (req == NULL || wpabuf_len(req) < sizeof(*hdr))
2042 return;
2043
2044 hdr = wpabuf_head(req);
2045 plen = be_to_host16(hdr->length);
2046 if (plen > wpabuf_len(req)) {
2047 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
2048 "(len=%lu plen=%lu)",
2049 (unsigned long) wpabuf_len(req),
2050 (unsigned long) plen);
2051 return;
2052 }
2053
2054 sm->reqId = hdr->identifier;
2055
2056 if (sm->workaround) {
2057 const u8 *addr[1];
2058 addr[0] = wpabuf_head(req);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002059 sha1_vector(1, addr, &plen, sm->req_sha1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 }
2061
2062 switch (hdr->code) {
2063 case EAP_CODE_REQUEST:
2064 if (plen < sizeof(*hdr) + 1) {
2065 wpa_printf(MSG_DEBUG, "EAP: Too short EAP-Request - "
2066 "no Type field");
2067 return;
2068 }
Hai Shalome21d4e82020-04-29 16:34:06 -07002069 sm->rxReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 pos = (const u8 *) (hdr + 1);
2071 sm->reqMethod = *pos++;
2072 if (sm->reqMethod == EAP_TYPE_EXPANDED) {
2073 if (plen < sizeof(*hdr) + 8) {
2074 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
2075 "expanded EAP-Packet (plen=%lu)",
2076 (unsigned long) plen);
2077 return;
2078 }
2079 sm->reqVendor = WPA_GET_BE24(pos);
2080 pos += 3;
2081 sm->reqVendorMethod = WPA_GET_BE32(pos);
2082 }
2083 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Request id=%d "
2084 "method=%u vendor=%u vendorMethod=%u",
2085 sm->reqId, sm->reqMethod, sm->reqVendor,
2086 sm->reqVendorMethod);
2087 break;
2088 case EAP_CODE_RESPONSE:
2089 if (sm->selectedMethod == EAP_TYPE_LEAP) {
2090 /*
2091 * LEAP differs from RFC 4137 by using reversed roles
2092 * for mutual authentication and because of this, we
2093 * need to accept EAP-Response frames if LEAP is used.
2094 */
2095 if (plen < sizeof(*hdr) + 1) {
2096 wpa_printf(MSG_DEBUG, "EAP: Too short "
2097 "EAP-Response - no Type field");
2098 return;
2099 }
Hai Shalome21d4e82020-04-29 16:34:06 -07002100 sm->rxResp = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 pos = (const u8 *) (hdr + 1);
2102 sm->reqMethod = *pos;
2103 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Response for "
2104 "LEAP method=%d id=%d",
2105 sm->reqMethod, sm->reqId);
2106 break;
2107 }
2108 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Response");
2109 break;
2110 case EAP_CODE_SUCCESS:
2111 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Success");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002112 eap_notify_status(sm, "completion", "success");
Hai Shalome21d4e82020-04-29 16:34:06 -07002113 sm->rxSuccess = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 break;
2115 case EAP_CODE_FAILURE:
2116 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Failure");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002117 eap_notify_status(sm, "completion", "failure");
Ahmed ElArabawy9c86a7f2018-03-15 09:00:10 -07002118
2119 /* Get the error code from method */
Ahmed ElArabawyab9f5af2018-04-04 18:56:43 -07002120 if (sm->m && sm->m->get_error_code) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002121 int error_code;
2122
Ahmed ElArabawy9c86a7f2018-03-15 09:00:10 -07002123 error_code = sm->m->get_error_code(sm->eap_method_priv);
2124 if (error_code != NO_EAP_METHOD_ERROR)
2125 eap_report_error(sm, error_code);
2126 }
Hai Shalome21d4e82020-04-29 16:34:06 -07002127 sm->rxFailure = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002129 case EAP_CODE_INITIATE:
2130 eap_peer_initiate(sm, hdr, plen);
2131 break;
2132 case EAP_CODE_FINISH:
2133 eap_peer_finish(sm, hdr, plen);
2134 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 default:
2136 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Packet with unknown "
2137 "code %d", hdr->code);
2138 break;
2139 }
2140}
2141
2142
2143static void eap_peer_sm_tls_event(void *ctx, enum tls_event ev,
2144 union tls_event_data *data)
2145{
2146 struct eap_sm *sm = ctx;
2147 char *hash_hex = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148
2149 switch (ev) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002150 case TLS_CERT_CHAIN_SUCCESS:
2151 eap_notify_status(sm, "remote certificate verification",
2152 "success");
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002153 if (sm->ext_cert_check) {
2154 sm->waiting_ext_cert_check = 1;
2155 eap_sm_request(sm, WPA_CTRL_REQ_EXT_CERT_CHECK,
2156 NULL, 0);
2157 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002158 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 case TLS_CERT_CHAIN_FAILURE:
2160 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_TLS_CERT_ERROR
2161 "reason=%d depth=%d subject='%s' err='%s'",
2162 data->cert_fail.reason,
2163 data->cert_fail.depth,
2164 data->cert_fail.subject,
2165 data->cert_fail.reason_txt);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002166 eap_notify_status(sm, "remote certificate verification",
2167 data->cert_fail.reason_txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168 break;
2169 case TLS_PEER_CERTIFICATE:
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07002170 if (!sm->eapol_cb->notify_cert)
2171 break;
2172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 if (data->peer_cert.hash) {
2174 size_t len = data->peer_cert.hash_len * 2 + 1;
2175 hash_hex = os_malloc(len);
2176 if (hash_hex) {
2177 wpa_snprintf_hex(hash_hex, len,
2178 data->peer_cert.hash,
2179 data->peer_cert.hash_len);
2180 }
2181 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182
Hai Shalom81f62d82019-07-22 12:10:00 -07002183 sm->eapol_cb->notify_cert(sm->eapol_ctx, &data->peer_cert,
2184 hash_hex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002186 case TLS_ALERT:
2187 if (data->alert.is_local)
2188 eap_notify_status(sm, "local TLS alert",
2189 data->alert.description);
2190 else
2191 eap_notify_status(sm, "remote TLS alert",
2192 data->alert.description);
2193 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07002194 case TLS_UNSAFE_RENEGOTIATION_DISABLED:
2195 wpa_printf(MSG_INFO,
2196 "TLS handshake failed due to the server not supporting safe renegotiation (RFC 5746); phase1 parameter allow_unsafe_renegotiation=1 can be used to work around this");
2197 eap_notify_status(sm, "unsafe server renegotiation", "failure");
2198 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 }
2200
2201 os_free(hash_hex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202}
2203
Gabriel Birena5bdf372022-12-15 20:54:33 +00002204ssize_t tls_certificate_callback(void* ctx, const char* alias, uint8_t** value) {
2205 if (alias == NULL || ctx == NULL || value == NULL) return -1;
2206 struct eap_sm *sm = (struct eap_sm*) ctx;
Gabriel Biren980c48a2023-03-27 21:49:21 +00002207 wpa_printf(MSG_INFO, "tls_certificate_callback: received sm=%p", (void*)sm);
Gabriel Birena5bdf372022-12-15 20:54:33 +00002208 if (sm->eapol_cb && sm->eapol_cb->get_certificate) {
2209 return sm->eapol_cb->get_certificate(sm->eapol_ctx, alias, value);
2210 }
2211 return -1;
2212}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213
Gabriel Biren60ae0682023-11-01 22:04:12 +00002214void tls_openssl_failure_callback(void* ctx, const char* msg) {
2215 if (ctx == NULL || msg == NULL) return;
2216 struct eap_sm *sm = (struct eap_sm*) ctx;
2217 if (sm->eapol_cb && sm->eapol_cb->notify_open_ssl_failure) {
2218 sm->eapol_cb->notify_open_ssl_failure(sm->eapol_ctx, msg);
2219 }
2220}
2221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222/**
2223 * eap_peer_sm_init - Allocate and initialize EAP peer state machine
2224 * @eapol_ctx: Context data to be used with eapol_cb calls
2225 * @eapol_cb: Pointer to EAPOL callback functions
2226 * @msg_ctx: Context data for wpa_msg() calls
2227 * @conf: EAP configuration
2228 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
2229 *
2230 * This function allocates and initializes an EAP state machine. In addition,
2231 * this initializes TLS library for the new EAP state machine. eapol_cb pointer
2232 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
2233 * state machine. Consequently, the caller must make sure that this data
2234 * structure remains alive while the EAP state machine is active.
2235 */
2236struct eap_sm * eap_peer_sm_init(void *eapol_ctx,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002237 const struct eapol_callbacks *eapol_cb,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 void *msg_ctx, struct eap_config *conf)
2239{
2240 struct eap_sm *sm;
2241 struct tls_config tlsconf;
2242
2243 sm = os_zalloc(sizeof(*sm));
Gabriel Biren980c48a2023-03-27 21:49:21 +00002244 wpa_printf(MSG_INFO, "Init sm=%p", (void*)sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 if (sm == NULL)
2246 return NULL;
2247 sm->eapol_ctx = eapol_ctx;
2248 sm->eapol_cb = eapol_cb;
2249 sm->msg_ctx = msg_ctx;
2250 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
2251 sm->wps = conf->wps;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002252 dl_list_init(&sm->erp_keys);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253
2254 os_memset(&tlsconf, 0, sizeof(tlsconf));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002255#ifndef CONFIG_OPENSC_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 tlsconf.opensc_engine_path = conf->opensc_engine_path;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002257#endif /* CONFIG_OPENSC_ENGINE_PATH */
2258#ifndef CONFIG_PKCS11_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259 tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002260#endif /* CONFIG_PKCS11_ENGINE_PATH */
2261#ifndef CONFIG_PKCS11_MODULE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 tlsconf.pkcs11_module_path = conf->pkcs11_module_path;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002263#endif /* CONFIG_PKCS11_MODULE_PATH */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002264 tlsconf.openssl_ciphers = conf->openssl_ciphers;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265#ifdef CONFIG_FIPS
2266 tlsconf.fips_mode = 1;
2267#endif /* CONFIG_FIPS */
2268 tlsconf.event_cb = eap_peer_sm_tls_event;
2269 tlsconf.cb_ctx = sm;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002270 tlsconf.cert_in_cb = conf->cert_in_cb;
Gabriel Birena5bdf372022-12-15 20:54:33 +00002271 tls_register_cert_callback(&tls_certificate_callback);
Gabriel Biren60ae0682023-11-01 22:04:12 +00002272 tls_register_openssl_failure_callback(&tls_openssl_failure_callback);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002273 sm->ssl_ctx = tls_init(&tlsconf);
2274 if (sm->ssl_ctx == NULL) {
2275 wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS "
2276 "context.");
2277 os_free(sm);
2278 return NULL;
2279 }
2280
Dmitry Shmidt04949592012-07-19 12:16:46 -07002281 sm->ssl_ctx2 = tls_init(&tlsconf);
2282 if (sm->ssl_ctx2 == NULL) {
2283 wpa_printf(MSG_INFO, "SSL: Failed to initialize TLS "
2284 "context (2).");
2285 /* Run without separate TLS context within TLS tunnel */
2286 }
2287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 return sm;
2289}
2290
2291
2292/**
2293 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
2294 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2295 *
2296 * This function deinitializes EAP state machine and frees all allocated
2297 * resources.
2298 */
2299void eap_peer_sm_deinit(struct eap_sm *sm)
2300{
Gabriel Biren980c48a2023-03-27 21:49:21 +00002301 wpa_printf(MSG_INFO, "Deinit sm=%p", (void*)sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 if (sm == NULL)
2303 return;
2304 eap_deinit_prev_method(sm, "EAP deinit");
2305 eap_sm_abort(sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002306 if (sm->ssl_ctx2)
2307 tls_deinit(sm->ssl_ctx2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308 tls_deinit(sm->ssl_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002309 eap_peer_erp_free_keys(sm);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002310 os_free(sm->identity);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 os_free(sm);
2312}
2313
2314
2315/**
2316 * eap_peer_sm_step - Step EAP peer state machine
2317 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2318 * Returns: 1 if EAP state was changed or 0 if not
2319 *
2320 * This function advances EAP state machine to a new state to match with the
2321 * current variables. This should be called whenever variables used by the EAP
2322 * state machine have changed.
2323 */
2324int eap_peer_sm_step(struct eap_sm *sm)
2325{
2326 int res = 0;
2327 do {
Hai Shalome21d4e82020-04-29 16:34:06 -07002328 sm->changed = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 SM_STEP_RUN(EAP);
2330 if (sm->changed)
2331 res = 1;
2332 } while (sm->changed);
2333 return res;
2334}
2335
2336
2337/**
2338 * eap_sm_abort - Abort EAP authentication
2339 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2340 *
2341 * Release system resources that have been allocated for the authentication
2342 * session without fully deinitializing the EAP state machine.
2343 */
2344void eap_sm_abort(struct eap_sm *sm)
2345{
2346 wpabuf_free(sm->lastRespData);
2347 sm->lastRespData = NULL;
2348 wpabuf_free(sm->eapRespData);
2349 sm->eapRespData = NULL;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002350 eap_sm_free_key(sm);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002351 os_free(sm->eapSessionId);
2352 sm->eapSessionId = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353
2354 /* This is not clearly specified in the EAP statemachines draft, but
2355 * it seems necessary to make sure that some of the EAPOL variables get
2356 * cleared for the next authentication. */
Hai Shalome21d4e82020-04-29 16:34:06 -07002357 eapol_set_bool(sm, EAPOL_eapSuccess, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358}
2359
2360
2361#ifdef CONFIG_CTRL_IFACE
2362static const char * eap_sm_state_txt(int state)
2363{
2364 switch (state) {
2365 case EAP_INITIALIZE:
2366 return "INITIALIZE";
2367 case EAP_DISABLED:
2368 return "DISABLED";
2369 case EAP_IDLE:
2370 return "IDLE";
2371 case EAP_RECEIVED:
2372 return "RECEIVED";
2373 case EAP_GET_METHOD:
2374 return "GET_METHOD";
2375 case EAP_METHOD:
2376 return "METHOD";
2377 case EAP_SEND_RESPONSE:
2378 return "SEND_RESPONSE";
2379 case EAP_DISCARD:
2380 return "DISCARD";
2381 case EAP_IDENTITY:
2382 return "IDENTITY";
2383 case EAP_NOTIFICATION:
2384 return "NOTIFICATION";
2385 case EAP_RETRANSMIT:
2386 return "RETRANSMIT";
2387 case EAP_SUCCESS:
2388 return "SUCCESS";
2389 case EAP_FAILURE:
2390 return "FAILURE";
2391 default:
2392 return "UNKNOWN";
2393 }
2394}
2395#endif /* CONFIG_CTRL_IFACE */
2396
2397
2398#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
2399static const char * eap_sm_method_state_txt(EapMethodState state)
2400{
2401 switch (state) {
2402 case METHOD_NONE:
2403 return "NONE";
2404 case METHOD_INIT:
2405 return "INIT";
2406 case METHOD_CONT:
2407 return "CONT";
2408 case METHOD_MAY_CONT:
2409 return "MAY_CONT";
2410 case METHOD_DONE:
2411 return "DONE";
2412 default:
2413 return "UNKNOWN";
2414 }
2415}
2416
2417
2418static const char * eap_sm_decision_txt(EapDecision decision)
2419{
2420 switch (decision) {
2421 case DECISION_FAIL:
2422 return "FAIL";
2423 case DECISION_COND_SUCC:
2424 return "COND_SUCC";
2425 case DECISION_UNCOND_SUCC:
2426 return "UNCOND_SUCC";
2427 default:
2428 return "UNKNOWN";
2429 }
2430}
2431#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
2432
2433
2434#ifdef CONFIG_CTRL_IFACE
2435
2436/**
2437 * eap_sm_get_status - Get EAP state machine status
2438 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2439 * @buf: Buffer for status information
2440 * @buflen: Maximum buffer length
2441 * @verbose: Whether to include verbose status information
2442 * Returns: Number of bytes written to buf.
2443 *
2444 * Query EAP state machine for status information. This function fills in a
2445 * text area with current status information from the EAPOL state machine. If
2446 * the buffer (buf) is not large enough, status information will be truncated
2447 * to fit the buffer.
2448 */
2449int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose)
2450{
2451 int len, ret;
2452
2453 if (sm == NULL)
2454 return 0;
2455
2456 len = os_snprintf(buf, buflen,
2457 "EAP state=%s\n",
2458 eap_sm_state_txt(sm->EAP_state));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002459 if (os_snprintf_error(buflen, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460 return 0;
2461
2462 if (sm->selectedMethod != EAP_TYPE_NONE) {
2463 const char *name;
2464 if (sm->m) {
2465 name = sm->m->name;
2466 } else {
2467 const struct eap_method *m =
2468 eap_peer_get_eap_method(EAP_VENDOR_IETF,
2469 sm->selectedMethod);
2470 if (m)
2471 name = m->name;
2472 else
2473 name = "?";
2474 }
2475 ret = os_snprintf(buf + len, buflen - len,
2476 "selectedMethod=%d (EAP-%s)\n",
2477 sm->selectedMethod, name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002478 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 return len;
2480 len += ret;
2481
2482 if (sm->m && sm->m->get_status) {
2483 len += sm->m->get_status(sm, sm->eap_method_priv,
2484 buf + len, buflen - len,
2485 verbose);
2486 }
2487 }
2488
2489 if (verbose) {
2490 ret = os_snprintf(buf + len, buflen - len,
2491 "reqMethod=%d\n"
2492 "methodState=%s\n"
2493 "decision=%s\n"
2494 "ClientTimeout=%d\n",
2495 sm->reqMethod,
2496 eap_sm_method_state_txt(sm->methodState),
2497 eap_sm_decision_txt(sm->decision),
2498 sm->ClientTimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002499 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 return len;
2501 len += ret;
2502 }
2503
2504 return len;
2505}
2506#endif /* CONFIG_CTRL_IFACE */
2507
2508
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002509static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 const char *msg, size_t msglen)
2511{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002512#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513 struct eap_peer_config *config;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002514 const char *txt = NULL;
2515 char *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516
2517 if (sm == NULL)
2518 return;
2519 config = eap_get_config(sm);
2520 if (config == NULL)
2521 return;
2522
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002523 switch (field) {
2524 case WPA_CTRL_REQ_EAP_IDENTITY:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525 config->pending_req_identity++;
2526 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002527 case WPA_CTRL_REQ_EAP_PASSWORD:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 config->pending_req_password++;
2529 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002530 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531 config->pending_req_new_password++;
2532 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002533 case WPA_CTRL_REQ_EAP_PIN:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 config->pending_req_pin++;
2535 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002536 case WPA_CTRL_REQ_EAP_OTP:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 if (msg) {
2538 tmp = os_malloc(msglen + 3);
2539 if (tmp == NULL)
2540 return;
2541 tmp[0] = '[';
2542 os_memcpy(tmp + 1, msg, msglen);
2543 tmp[msglen + 1] = ']';
2544 tmp[msglen + 2] = '\0';
2545 txt = tmp;
2546 os_free(config->pending_req_otp);
2547 config->pending_req_otp = tmp;
2548 config->pending_req_otp_len = msglen + 3;
2549 } else {
2550 if (config->pending_req_otp == NULL)
2551 return;
2552 txt = config->pending_req_otp;
2553 }
2554 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002555 case WPA_CTRL_REQ_EAP_PASSPHRASE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 config->pending_req_passphrase++;
2557 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002558 case WPA_CTRL_REQ_SIM:
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002559 config->pending_req_sim++;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002560 txt = msg;
2561 break;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002562 case WPA_CTRL_REQ_EXT_CERT_CHECK:
2563 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564 default:
2565 return;
2566 }
2567
2568 if (sm->eapol_cb->eap_param_needed)
2569 sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002571}
2572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573
2574const char * eap_sm_get_method_name(struct eap_sm *sm)
2575{
2576 if (sm->m == NULL)
2577 return "UNKNOWN";
2578 return sm->m->name;
2579}
2580
2581
2582/**
2583 * eap_sm_request_identity - Request identity from user (ctrl_iface)
2584 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2585 *
2586 * EAP methods can call this function to request identity information for the
2587 * current network. This is normally called when the identity is not included
2588 * in the network configuration. The request will be sent to monitor programs
2589 * through the control interface.
2590 */
2591void eap_sm_request_identity(struct eap_sm *sm)
2592{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002593 eap_sm_request(sm, WPA_CTRL_REQ_EAP_IDENTITY, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594}
2595
2596
2597/**
2598 * eap_sm_request_password - Request password from user (ctrl_iface)
2599 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2600 *
2601 * EAP methods can call this function to request password information for the
2602 * current network. This is normally called when the password is not included
2603 * in the network configuration. The request will be sent to monitor programs
2604 * through the control interface.
2605 */
2606void eap_sm_request_password(struct eap_sm *sm)
2607{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002608 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSWORD, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609}
2610
2611
2612/**
2613 * eap_sm_request_new_password - Request new password from user (ctrl_iface)
2614 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2615 *
2616 * EAP methods can call this function to request new password information for
2617 * the current network. This is normally called when the EAP method indicates
2618 * that the current password has expired and password change is required. The
2619 * request will be sent to monitor programs through the control interface.
2620 */
2621void eap_sm_request_new_password(struct eap_sm *sm)
2622{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002623 eap_sm_request(sm, WPA_CTRL_REQ_EAP_NEW_PASSWORD, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624}
2625
2626
2627/**
2628 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
2629 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2630 *
2631 * EAP methods can call this function to request SIM or smart card PIN
2632 * information for the current network. This is normally called when the PIN is
2633 * not included in the network configuration. The request will be sent to
2634 * monitor programs through the control interface.
2635 */
2636void eap_sm_request_pin(struct eap_sm *sm)
2637{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002638 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PIN, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639}
2640
2641
2642/**
2643 * eap_sm_request_otp - Request one time password from user (ctrl_iface)
2644 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2645 * @msg: Message to be displayed to the user when asking for OTP
2646 * @msg_len: Length of the user displayable message
2647 *
2648 * EAP methods can call this function to request open time password (OTP) for
2649 * the current network. The request will be sent to monitor programs through
2650 * the control interface.
2651 */
2652void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len)
2653{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002654 eap_sm_request(sm, WPA_CTRL_REQ_EAP_OTP, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002655}
2656
2657
2658/**
2659 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
2660 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2661 *
2662 * EAP methods can call this function to request passphrase for a private key
2663 * for the current network. This is normally called when the passphrase is not
2664 * included in the network configuration. The request will be sent to monitor
2665 * programs through the control interface.
2666 */
2667void eap_sm_request_passphrase(struct eap_sm *sm)
2668{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002669 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSPHRASE, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670}
2671
2672
2673/**
Dmitry Shmidt051af732013-10-22 13:52:46 -07002674 * eap_sm_request_sim - Request external SIM processing
2675 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2676 * @req: EAP method specific request
2677 */
2678void eap_sm_request_sim(struct eap_sm *sm, const char *req)
2679{
2680 eap_sm_request(sm, WPA_CTRL_REQ_SIM, req, os_strlen(req));
2681}
2682
2683
2684/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685 * eap_sm_notify_ctrl_attached - Notification of attached monitor
2686 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2687 *
2688 * Notify EAP state machines that a monitor was attached to the control
2689 * interface to trigger re-sending of pending requests for user input.
2690 */
2691void eap_sm_notify_ctrl_attached(struct eap_sm *sm)
2692{
2693 struct eap_peer_config *config = eap_get_config(sm);
2694
2695 if (config == NULL)
2696 return;
2697
2698 /* Re-send any pending requests for user data since a new control
2699 * interface was added. This handles cases where the EAP authentication
2700 * starts immediately after system startup when the user interface is
2701 * not yet running. */
2702 if (config->pending_req_identity)
2703 eap_sm_request_identity(sm);
2704 if (config->pending_req_password)
2705 eap_sm_request_password(sm);
2706 if (config->pending_req_new_password)
2707 eap_sm_request_new_password(sm);
2708 if (config->pending_req_otp)
2709 eap_sm_request_otp(sm, NULL, 0);
2710 if (config->pending_req_pin)
2711 eap_sm_request_pin(sm);
2712 if (config->pending_req_passphrase)
2713 eap_sm_request_passphrase(sm);
2714}
2715
2716
2717static int eap_allowed_phase2_type(int vendor, int type)
2718{
Hai Shalomc3565922019-10-28 11:58:20 -07002719 if (vendor == EAP_VENDOR_HOSTAP)
2720 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721 if (vendor != EAP_VENDOR_IETF)
2722 return 0;
2723 return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS &&
Hai Shalom81f62d82019-07-22 12:10:00 -07002724 type != EAP_TYPE_FAST && type != EAP_TYPE_TEAP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002725}
2726
2727
2728/**
2729 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
2730 * @name: EAP method name, e.g., MD5
2731 * @vendor: Buffer for returning EAP Vendor-Id
2732 * Returns: EAP method type or %EAP_TYPE_NONE if not found
2733 *
2734 * This function maps EAP type names into EAP type numbers that are allowed for
2735 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
2736 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
2737 */
2738u32 eap_get_phase2_type(const char *name, int *vendor)
2739{
2740 int v;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07002741 u32 type = eap_peer_get_type(name, &v);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742 if (eap_allowed_phase2_type(v, type)) {
2743 *vendor = v;
2744 return type;
2745 }
2746 *vendor = EAP_VENDOR_IETF;
2747 return EAP_TYPE_NONE;
2748}
2749
2750
2751/**
2752 * eap_get_phase2_types - Get list of allowed EAP phase 2 types
2753 * @config: Pointer to a network configuration
2754 * @count: Pointer to a variable to be filled with number of returned EAP types
2755 * Returns: Pointer to allocated type list or %NULL on failure
2756 *
2757 * This function generates an array of allowed EAP phase 2 (tunneled) types for
2758 * the given network configuration.
2759 */
2760struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config,
2761 size_t *count)
2762{
2763 struct eap_method_type *buf;
2764 u32 method;
2765 int vendor;
2766 size_t mcount;
2767 const struct eap_method *methods, *m;
2768
2769 methods = eap_peer_get_methods(&mcount);
2770 if (methods == NULL)
2771 return NULL;
2772 *count = 0;
2773 buf = os_malloc(mcount * sizeof(struct eap_method_type));
2774 if (buf == NULL)
2775 return NULL;
2776
2777 for (m = methods; m; m = m->next) {
2778 vendor = m->vendor;
2779 method = m->method;
2780 if (eap_allowed_phase2_type(vendor, method)) {
2781 if (vendor == EAP_VENDOR_IETF &&
2782 method == EAP_TYPE_TLS && config &&
Hai Shalomc3565922019-10-28 11:58:20 -07002783 !config->phase2_cert.private_key)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784 continue;
2785 buf[*count].vendor = vendor;
2786 buf[*count].method = method;
2787 (*count)++;
2788 }
2789 }
2790
2791 return buf;
2792}
2793
2794
2795/**
2796 * eap_set_fast_reauth - Update fast_reauth setting
2797 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2798 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
2799 */
2800void eap_set_fast_reauth(struct eap_sm *sm, int enabled)
2801{
2802 sm->fast_reauth = enabled;
2803}
2804
2805
2806/**
2807 * eap_set_workaround - Update EAP workarounds setting
2808 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2809 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
2810 */
2811void eap_set_workaround(struct eap_sm *sm, unsigned int workaround)
2812{
2813 sm->workaround = workaround;
2814}
2815
2816
2817/**
2818 * eap_get_config - Get current network configuration
2819 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2820 * Returns: Pointer to the current network configuration or %NULL if not found
2821 *
2822 * EAP peer methods should avoid using this function if they can use other
2823 * access functions, like eap_get_config_identity() and
2824 * eap_get_config_password(), that do not require direct access to
2825 * struct eap_peer_config.
2826 */
2827struct eap_peer_config * eap_get_config(struct eap_sm *sm)
2828{
2829 return sm->eapol_cb->get_config(sm->eapol_ctx);
2830}
2831
2832
2833/**
2834 * eap_get_config_identity - Get identity from the network configuration
2835 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2836 * @len: Buffer for the length of the identity
2837 * Returns: Pointer to the identity or %NULL if not found
2838 */
2839const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
2840{
2841 struct eap_peer_config *config = eap_get_config(sm);
Hai Shalomc3565922019-10-28 11:58:20 -07002842
2843 if (!config)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844 return NULL;
Hai Shalomc3565922019-10-28 11:58:20 -07002845
2846 if (sm->use_machine_cred) {
2847 *len = config->machine_identity_len;
2848 return config->machine_identity;
2849 }
2850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851 *len = config->identity_len;
2852 return config->identity;
2853}
2854
Steven Liu9138d432022-11-23 22:29:05 +00002855
2856/**
2857 * eap_get_config_strict_conservative_peer_mode - get the value of
2858 * strict conservative peer mode in eap_peer_config.
2859 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2860*/
2861int eap_get_config_strict_conservative_peer_mode(struct eap_sm *sm)
2862{
2863 struct eap_peer_config *config;
2864 config = eap_get_config(sm);
2865 if (config) {
2866 return config->strict_conservative_peer_mode;
2867 }
2868
2869 return 0;
2870}
2871
2872
Jimmy Chen1e9d9152022-08-09 23:21:26 +08002873static const u8 * strnchr(const u8 *str, size_t len, u8 needle) {
2874 const u8 *cur = str;
2875
2876 if (NULL == str) return NULL;
2877 if (0 >= len) return NULL;
2878
2879 while (cur < str + len) {
2880 if (*cur == needle)
2881 return cur;
2882 cur++;
2883 }
2884 return NULL;
2885}
2886
2887const u8 * eap_get_config_realm(struct eap_sm *sm, size_t *len) {
2888 struct eap_peer_config *config = eap_get_config(sm);
2889 const u8 *realm = NULL;
2890 size_t realm_len = 0;
2891 const u8 *identity = NULL;
2892 size_t identity_len = 0;
2893
2894 if (!config)
2895 return NULL;
2896
2897 /* Look for the realm of the permanent identity */
2898 identity = eap_get_config_identity(sm, &identity_len);
2899 realm = strnchr(identity, identity_len, '@');
2900 if (NULL != realm) {
2901 wpa_printf(MSG_DEBUG, "Get the realm from identity.");
2902 *len = identity_len - (realm - identity);
2903 return realm;
2904 }
2905
2906 /* Look for the realm of the anonymous identity. */
Jimmy Chen1651ba62022-08-31 18:47:46 +08002907 identity = config->anonymous_identity;
2908 identity_len = config->anonymous_identity_len;
2909 realm = strnchr(identity, identity_len, '@');
Jimmy Chen1e9d9152022-08-09 23:21:26 +08002910 if (NULL != realm) {
2911 wpa_printf(MSG_DEBUG, "Get the realm from anonymous identity.");
2912 *len = identity_len - (realm - identity);
2913 return realm;
2914 }
2915
2916 /* Look for the realm of the real identity. */
Jimmy Chen1651ba62022-08-31 18:47:46 +08002917 identity = config->imsi_identity;
2918 identity_len = config->imsi_identity_len;
2919 realm = strnchr(identity, identity_len, '@');
Jimmy Chen1e9d9152022-08-09 23:21:26 +08002920 if (NULL != realm) {
2921 wpa_printf(MSG_DEBUG, "Get the realm from IMSI identity.");
2922 *len = identity_len - (realm - identity);
2923 return realm;
2924 }
2925 wpa_printf(MSG_DEBUG, "No realm information in identities.");
2926 *len = 0;
2927 return NULL;
2928}
2929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002931static int eap_get_ext_password(struct eap_sm *sm,
2932 struct eap_peer_config *config)
2933{
2934 char *name;
Hai Shalomc3565922019-10-28 11:58:20 -07002935 const u8 *password;
2936 size_t password_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002937
Hai Shalomc3565922019-10-28 11:58:20 -07002938 if (sm->use_machine_cred) {
2939 password = config->machine_password;
2940 password_len = config->machine_password_len;
2941 } else {
2942 password = config->password;
2943 password_len = config->password_len;
2944 }
2945
2946 if (!password)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002947 return -1;
2948
Hai Shalomc3565922019-10-28 11:58:20 -07002949 name = os_zalloc(password_len + 1);
2950 if (!name)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002951 return -1;
Hai Shalomc3565922019-10-28 11:58:20 -07002952 os_memcpy(name, password, password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002953
2954 ext_password_free(sm->ext_pw_buf);
2955 sm->ext_pw_buf = ext_password_get(sm->ext_pw, name);
2956 os_free(name);
2957
2958 return sm->ext_pw_buf == NULL ? -1 : 0;
2959}
2960
2961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962/**
2963 * eap_get_config_password - Get password from the network configuration
2964 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2965 * @len: Buffer for the length of the password
2966 * Returns: Pointer to the password or %NULL if not found
2967 */
2968const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
2969{
2970 struct eap_peer_config *config = eap_get_config(sm);
Hai Shalomc3565922019-10-28 11:58:20 -07002971
2972 if (!config)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002973 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002974
Hai Shalomc3565922019-10-28 11:58:20 -07002975 if ((sm->use_machine_cred &&
2976 (config->flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD)) ||
2977 (!sm->use_machine_cred &&
2978 (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD))) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002979 if (eap_get_ext_password(sm, config) < 0)
2980 return NULL;
2981 *len = wpabuf_len(sm->ext_pw_buf);
2982 return wpabuf_head(sm->ext_pw_buf);
2983 }
2984
Hai Shalomc3565922019-10-28 11:58:20 -07002985 if (sm->use_machine_cred) {
2986 *len = config->machine_password_len;
2987 return config->machine_password;
2988 }
2989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002990 *len = config->password_len;
2991 return config->password;
2992}
2993
2994
2995/**
2996 * eap_get_config_password2 - Get password from the network configuration
2997 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2998 * @len: Buffer for the length of the password
2999 * @hash: Buffer for returning whether the password is stored as a
3000 * NtPasswordHash instead of plaintext password; can be %NULL if this
3001 * information is not needed
3002 * Returns: Pointer to the password or %NULL if not found
3003 */
3004const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
3005{
3006 struct eap_peer_config *config = eap_get_config(sm);
Hai Shalomc3565922019-10-28 11:58:20 -07003007
3008 if (!config)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003009 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003010
Hai Shalomc3565922019-10-28 11:58:20 -07003011 if ((sm->use_machine_cred &&
3012 (config->flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD)) ||
3013 (!sm->use_machine_cred &&
3014 (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD))) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003015 if (eap_get_ext_password(sm, config) < 0)
3016 return NULL;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003017 if (hash)
3018 *hash = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003019 *len = wpabuf_len(sm->ext_pw_buf);
3020 return wpabuf_head(sm->ext_pw_buf);
3021 }
3022
Hai Shalomc3565922019-10-28 11:58:20 -07003023 if (sm->use_machine_cred) {
3024 *len = config->machine_password_len;
3025 if (hash)
3026 *hash = !!(config->flags &
3027 EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH);
3028 return config->machine_password;
3029 }
3030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 *len = config->password_len;
3032 if (hash)
3033 *hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH);
3034 return config->password;
3035}
3036
3037
3038/**
3039 * eap_get_config_new_password - Get new password from network configuration
3040 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3041 * @len: Buffer for the length of the new password
3042 * Returns: Pointer to the new password or %NULL if not found
3043 */
3044const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
3045{
3046 struct eap_peer_config *config = eap_get_config(sm);
3047 if (config == NULL)
3048 return NULL;
3049 *len = config->new_password_len;
3050 return config->new_password;
3051}
3052
3053
3054/**
3055 * eap_get_config_otp - Get one-time password from the network configuration
3056 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3057 * @len: Buffer for the length of the one-time password
3058 * Returns: Pointer to the one-time password or %NULL if not found
3059 */
3060const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len)
3061{
3062 struct eap_peer_config *config = eap_get_config(sm);
3063 if (config == NULL)
3064 return NULL;
3065 *len = config->otp_len;
3066 return config->otp;
3067}
3068
3069
3070/**
3071 * eap_clear_config_otp - Clear used one-time password
3072 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3073 *
3074 * This function clears a used one-time password (OTP) from the current network
3075 * configuration. This should be called when the OTP has been used and is not
3076 * needed anymore.
3077 */
3078void eap_clear_config_otp(struct eap_sm *sm)
3079{
3080 struct eap_peer_config *config = eap_get_config(sm);
3081 if (config == NULL)
3082 return;
3083 os_memset(config->otp, 0, config->otp_len);
3084 os_free(config->otp);
3085 config->otp = NULL;
3086 config->otp_len = 0;
3087}
3088
3089
3090/**
3091 * eap_get_config_phase1 - Get phase1 data from the network configuration
3092 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3093 * Returns: Pointer to the phase1 data or %NULL if not found
3094 */
3095const char * eap_get_config_phase1(struct eap_sm *sm)
3096{
3097 struct eap_peer_config *config = eap_get_config(sm);
3098 if (config == NULL)
3099 return NULL;
3100 return config->phase1;
3101}
3102
3103
3104/**
3105 * eap_get_config_phase2 - Get phase2 data from the network configuration
3106 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3107 * Returns: Pointer to the phase1 data or %NULL if not found
3108 */
3109const char * eap_get_config_phase2(struct eap_sm *sm)
3110{
3111 struct eap_peer_config *config = eap_get_config(sm);
3112 if (config == NULL)
3113 return NULL;
3114 return config->phase2;
3115}
3116
3117
3118int eap_get_config_fragment_size(struct eap_sm *sm)
3119{
3120 struct eap_peer_config *config = eap_get_config(sm);
3121 if (config == NULL)
3122 return -1;
3123 return config->fragment_size;
3124}
3125
3126
3127/**
3128 * eap_key_available - Get key availability (eapKeyAvailable variable)
3129 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3130 * Returns: 1 if EAP keying material is available, 0 if not
3131 */
3132int eap_key_available(struct eap_sm *sm)
3133{
3134 return sm ? sm->eapKeyAvailable : 0;
3135}
3136
Steven Liu850c2e02022-11-28 17:26:39 +00003137/**
3138 * eap_notify_permanent_id_req_denied - Notify that the AT_PERMANENT_ID_REQ
3139 * is denied from eap_peer when the strict conservative mode is enabled.
3140 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3141*/
3142void eap_notify_permanent_id_req_denied(struct eap_sm *sm)
3143{
3144 if (!sm || !sm->eapol_cb->notify_permanent_id_req_denied)
3145 return;
3146
3147 sm->eapol_cb->notify_permanent_id_req_denied(sm->eapol_ctx);
3148}
3149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150
3151/**
3152 * eap_notify_success - Notify EAP state machine about external success trigger
3153 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3154 *
3155 * This function is called when external event, e.g., successful completion of
3156 * WPA-PSK key handshake, is indicating that EAP state machine should move to
3157 * success state. This is mainly used with security modes that do not use EAP
3158 * state machine (e.g., WPA-PSK).
3159 */
3160void eap_notify_success(struct eap_sm *sm)
3161{
3162 if (sm) {
3163 sm->decision = DECISION_COND_SUCC;
3164 sm->EAP_state = EAP_SUCCESS;
3165 }
3166}
3167
3168
3169/**
3170 * eap_notify_lower_layer_success - Notification of lower layer success
3171 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3172 *
3173 * Notify EAP state machines that a lower layer has detected a successful
3174 * authentication. This is used to recover from dropped EAP-Success messages.
3175 */
3176void eap_notify_lower_layer_success(struct eap_sm *sm)
3177{
3178 if (sm == NULL)
3179 return;
3180
3181 if (eapol_get_bool(sm, EAPOL_eapSuccess) ||
3182 sm->decision == DECISION_FAIL ||
3183 (sm->methodState != METHOD_MAY_CONT &&
3184 sm->methodState != METHOD_DONE))
3185 return;
3186
3187 if (sm->eapKeyData != NULL)
Hai Shalome21d4e82020-04-29 16:34:06 -07003188 sm->eapKeyAvailable = true;
3189 eapol_set_bool(sm, EAPOL_eapSuccess, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
3191 "EAP authentication completed successfully (based on lower "
3192 "layer success)");
3193}
3194
3195
3196/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003197 * eap_get_eapSessionId - Get Session-Id from EAP state machine
3198 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3199 * @len: Pointer to variable that will be set to number of bytes in the session
3200 * Returns: Pointer to the EAP Session-Id or %NULL on failure
3201 *
3202 * Fetch EAP Session-Id from the EAP state machine. The Session-Id is available
3203 * only after a successful authentication. EAP state machine continues to manage
3204 * the Session-Id and the caller must not change or free the returned data.
3205 */
3206const u8 * eap_get_eapSessionId(struct eap_sm *sm, size_t *len)
3207{
3208 if (sm == NULL || sm->eapSessionId == NULL) {
3209 *len = 0;
3210 return NULL;
3211 }
3212
3213 *len = sm->eapSessionIdLen;
3214 return sm->eapSessionId;
3215}
3216
3217
3218/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
3220 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3221 * @len: Pointer to variable that will be set to number of bytes in the key
3222 * Returns: Pointer to the EAP keying data or %NULL on failure
3223 *
3224 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
3225 * key is available only after a successful authentication. EAP state machine
3226 * continues to manage the key data and the caller must not change or free the
3227 * returned data.
3228 */
3229const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len)
3230{
3231 if (sm == NULL || sm->eapKeyData == NULL) {
3232 *len = 0;
3233 return NULL;
3234 }
3235
3236 *len = sm->eapKeyDataLen;
3237 return sm->eapKeyData;
3238}
3239
3240
3241/**
3242 * eap_get_eapKeyData - Get EAP response data
3243 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3244 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
3245 *
3246 * Fetch EAP response (eapRespData) from the EAP state machine. This data is
3247 * available when EAP state machine has processed an incoming EAP request. The
3248 * EAP state machine does not maintain a reference to the response after this
3249 * function is called and the caller is responsible for freeing the data.
3250 */
3251struct wpabuf * eap_get_eapRespData(struct eap_sm *sm)
3252{
3253 struct wpabuf *resp;
3254
3255 if (sm == NULL || sm->eapRespData == NULL)
3256 return NULL;
3257
3258 resp = sm->eapRespData;
3259 sm->eapRespData = NULL;
3260
3261 return resp;
3262}
3263
3264
3265/**
3266 * eap_sm_register_scard_ctx - Notification of smart card context
3267 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3268 * @ctx: Context data for smart card operations
3269 *
3270 * Notify EAP state machines of context data for smart card operations. This
3271 * context data will be used as a parameter for scard_*() functions.
3272 */
3273void eap_register_scard_ctx(struct eap_sm *sm, void *ctx)
3274{
3275 if (sm)
3276 sm->scard_ctx = ctx;
3277}
3278
3279
3280/**
3281 * eap_set_config_blob - Set or add a named configuration blob
3282 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3283 * @blob: New value for the blob
3284 *
3285 * Adds a new configuration blob or replaces the current value of an existing
3286 * blob.
3287 */
3288void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob)
3289{
3290#ifndef CONFIG_NO_CONFIG_BLOBS
3291 sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob);
3292#endif /* CONFIG_NO_CONFIG_BLOBS */
3293}
3294
3295
3296/**
3297 * eap_get_config_blob - Get a named configuration blob
3298 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3299 * @name: Name of the blob
3300 * Returns: Pointer to blob data or %NULL if not found
3301 */
3302const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm,
3303 const char *name)
3304{
3305#ifndef CONFIG_NO_CONFIG_BLOBS
3306 return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name);
3307#else /* CONFIG_NO_CONFIG_BLOBS */
3308 return NULL;
3309#endif /* CONFIG_NO_CONFIG_BLOBS */
3310}
3311
3312
3313/**
3314 * eap_set_force_disabled - Set force_disabled flag
3315 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3316 * @disabled: 1 = EAP disabled, 0 = EAP enabled
3317 *
3318 * This function is used to force EAP state machine to be disabled when it is
3319 * not in use (e.g., with WPA-PSK or plaintext connections).
3320 */
3321void eap_set_force_disabled(struct eap_sm *sm, int disabled)
3322{
3323 sm->force_disabled = disabled;
3324}
3325
3326
Dmitry Shmidt051af732013-10-22 13:52:46 -07003327/**
3328 * eap_set_external_sim - Set external_sim flag
3329 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3330 * @external_sim: Whether external SIM/USIM processing is used
3331 */
3332void eap_set_external_sim(struct eap_sm *sm, int external_sim)
3333{
3334 sm->external_sim = external_sim;
3335}
3336
3337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338 /**
3339 * eap_notify_pending - Notify that EAP method is ready to re-process a request
3340 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3341 *
3342 * An EAP method can perform a pending operation (e.g., to get a response from
3343 * an external process). Once the response is available, this function can be
3344 * used to request EAPOL state machine to retry delivering the previously
3345 * received (and still unanswered) EAP request to EAP state machine.
3346 */
3347void eap_notify_pending(struct eap_sm *sm)
3348{
3349 sm->eapol_cb->notify_pending(sm->eapol_ctx);
3350}
3351
3352
3353/**
3354 * eap_invalidate_cached_session - Mark cached session data invalid
3355 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3356 */
3357void eap_invalidate_cached_session(struct eap_sm *sm)
3358{
3359 if (sm)
3360 eap_deinit_prev_method(sm, "invalidate");
3361}
3362
3363
3364int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf)
3365{
3366 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
3367 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
3368 return 0; /* Not a WPS Enrollee */
3369
3370 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL)
3371 return 0; /* Not using PBC */
3372
3373 return 1;
3374}
3375
3376
3377int eap_is_wps_pin_enrollee(struct eap_peer_config *conf)
3378{
3379 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
3380 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
3381 return 0; /* Not a WPS Enrollee */
3382
3383 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL)
3384 return 0; /* Not using PIN */
3385
3386 return 1;
3387}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003388
3389
3390void eap_sm_set_ext_pw_ctx(struct eap_sm *sm, struct ext_password_data *ext)
3391{
3392 ext_password_free(sm->ext_pw_buf);
3393 sm->ext_pw_buf = NULL;
3394 sm->ext_pw = ext;
3395}
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003396
3397
3398/**
3399 * eap_set_anon_id - Set or add anonymous identity
3400 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3401 * @id: Anonymous identity (e.g., EAP-SIM pseudonym) or %NULL to clear
3402 * @len: Length of anonymous identity in octets
3403 */
3404void eap_set_anon_id(struct eap_sm *sm, const u8 *id, size_t len)
3405{
3406 if (sm->eapol_cb->set_anon_id)
3407 sm->eapol_cb->set_anon_id(sm->eapol_ctx, id, len);
3408}
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003409
3410
3411int eap_peer_was_failure_expected(struct eap_sm *sm)
3412{
3413 return sm->expected_failure;
3414}