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