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