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