2 * Copyright (c) 2010, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "gssapiP_eap.h"
35 #define EAP_MAX_METHODS 8
37 #define EAP_TTLS_AUTH_PAP 1
38 #define EAP_TTLS_AUTH_CHAP 2
39 #define EAP_TTLS_AUTH_MSCHAP 4
40 #define EAP_TTLS_AUTH_MSCHAPV2 8
47 } methods[EAP_MAX_METHODS];
50 int password_hash; /* whether password is hashed with
51 * nt_password_hash() */
54 int ttls_auth; /* bitfield of
55 * EAP_TTLS_AUTH_{PAP,CHAP,MSCHAP,MSCHAPV2} */
58 struct eap_eapol_interface {
59 /* Lower layer to full authenticator variables */
60 Boolean eapResp; /* shared with EAPOL Backend Authentication */
61 struct wpabuf *eapRespData;
64 Boolean eapRestart; /* shared with EAPOL Authenticator PAE */
68 /* Full authenticator to lower layer variables */
69 Boolean eapReq; /* shared with EAPOL Backend Authentication */
70 Boolean eapNoReq; /* shared with EAPOL Backend Authentication */
74 struct wpabuf *eapReqData;
77 Boolean eapKeyAvailable; /* called keyAvailable in IEEE 802.1X-2004 */
79 /* AAA interface to full authenticator variables */
84 struct wpabuf *aaaEapReqData;
86 size_t aaaEapKeyDataLen;
87 Boolean aaaEapKeyAvailable;
90 /* Full authenticator to AAA interface variables */
92 struct wpabuf *aaaEapRespData;
93 /* aaaIdentity -> eap_get_identity() */
97 #define eapol_callbacks SERVER_eapol_callbacks
99 struct eapol_callbacks {
100 int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
101 int phase2, struct eap_user *user);
102 const char * (*get_eap_req_id_text)(void *ctx, size_t *len);
105 #define eap_config SERVER_eap_config
110 void *eap_sim_db_priv;
111 Boolean backend_auth;
113 u8 *pac_opaque_encr_key;
115 size_t eap_fast_a_id_len;
116 char *eap_fast_a_id_info;
118 int pac_key_lifetime;
119 int pac_key_refresh_time;
120 int eap_sim_aka_result_ind;
122 struct wps_context *wps;
123 const struct wpabuf *assoc_wps_ie;
128 struct eap_sm * eap_server_sm_init(void *eapol_ctx,
129 struct eapol_callbacks *eapol_cb,
130 struct eap_config *eap_conf);
131 void eap_server_sm_deinit(struct eap_sm *sm);
132 int eap_server_sm_step(struct eap_sm *sm);
133 void eap_sm_notify_cached(struct eap_sm *sm);
134 void eap_sm_pending_cb(struct eap_sm *sm);
135 int eap_sm_method_pending(struct eap_sm *sm);
136 const u8 * eap_get_identity(struct eap_sm *sm, size_t *len);
137 struct eap_eapol_interface * eap_get_interface(struct eap_sm *sm);
139 #include <eap_server/eap_i.h>
142 initTls(OM_uint32 *minor,
145 struct tls_config tconf;
146 struct tls_connection_params tparams;
148 memset(&tconf, 0, sizeof(tconf));
149 ctx->acceptorCtx.tlsContext = tls_init(&tconf);
150 if (ctx->acceptorCtx.tlsContext == NULL)
151 return GSS_S_FAILURE;
153 memset(&tparams, 0, sizeof(tparams));
154 tparams.ca_cert = "ca.pem";
155 tparams.client_cert = "server.pem";
156 tparams.private_key = "server-key.pem";
158 if (tls_global_set_params(ctx->acceptorCtx.tlsContext, &tparams)) {
159 return GSS_S_FAILURE;
162 if (tls_global_set_verify(ctx->acceptorCtx.tlsContext, 0)) {
163 return GSS_S_FAILURE;
166 return GSS_S_COMPLETE;
170 serverGetEapUser(void *ctx,
171 const unsigned char *identity,
172 size_t identityLength,
174 struct eap_user *user)
176 gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx;
177 OM_uint32 major, minor;
180 memset(user, 0, sizeof(*user));
182 buf.length = identityLength;
183 buf.value = (void *)identity;
186 user->methods[0].vendor = EAP_VENDOR_IETF;
187 user->methods[0].method = EAP_TYPE_PEAP;
191 major = gssEapImportName(&minor, &buf, GSS_C_NT_USER_NAME,
192 &gssCtx->initiatorName);
193 if (GSS_ERROR(major))
197 * OK, obviously there is no real security here, this is simply
198 * for testing the token exchange; this code will be completely
199 * replaced with libradsec once that library is available.
201 user->methods[0].vendor = EAP_VENDOR_IETF;
202 user->methods[0].method = EAP_TYPE_MSCHAPV2;
203 user->password = (unsigned char *)strdup(" ");
204 user->password_len = 1;
210 serverGetEapReqIdText(void *ctx,
219 acceptReady(OM_uint32 *minor, gss_ctx_id_t ctx)
222 krb5_context krbContext;
224 GSSEAP_KRB_INIT(&krbContext);
226 /* Cache encryption type derived from selected mechanism OID */
227 major = gssEapOidToEnctype(minor, ctx->mechanismUsed, &ctx->encryptionType);
228 if (GSS_ERROR(major))
231 if (ctx->encryptionType != ENCTYPE_NULL &&
232 ctx->acceptorCtx.eapPolInterface->eapKeyAvailable) {
233 major = gssEapDeriveRFC3961Key(minor,
234 ctx->acceptorCtx.eapPolInterface->eapKeyData,
235 ctx->acceptorCtx.eapPolInterface->eapKeyDataLen,
238 if (GSS_ERROR(major))
242 * draft-howlett-eap-gss says that integrity/confidentialty should
243 * always be advertised as available, but if we have no keying
244 * material it seems confusing to the caller to advertise this.
246 ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
249 sequenceInit(&ctx->seqState, ctx->recvSeq,
250 ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
251 ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
254 return GSS_S_COMPLETE;
258 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
261 gss_buffer_t inputToken,
262 gss_channel_bindings_t chanBindings,
263 gss_buffer_t outputToken)
266 OM_uint32 tmpMinor, tmpMajor;
268 struct wpabuf respData;
269 static struct eapol_callbacks cb = { serverGetEapUser, serverGetEapReqIdText };
270 if (ctx->acceptorCtx.eap == NULL) {
271 struct eap_config eapConfig;
273 major = initTls(minor, ctx);
274 if (GSS_ERROR(major))
277 memset(&eapConfig, 0, sizeof(eapConfig));
278 eapConfig.eap_server = 1;
279 eapConfig.ssl_ctx = ctx->acceptorCtx.tlsContext;
281 ctx->acceptorCtx.eap = eap_server_sm_init(ctx, &cb, &eapConfig);
282 if (ctx->acceptorCtx.eap == NULL) {
283 major = GSS_S_FAILURE;
287 ctx->acceptorCtx.eapPolInterface = eap_get_interface(ctx->acceptorCtx.eap);
288 ctx->acceptorCtx.eapPolInterface->portEnabled = TRUE;
289 ctx->acceptorCtx.eapPolInterface->eapRestart = TRUE;
292 if (ctx->acceptorName == GSS_C_NO_NAME &&
293 cred != GSS_C_NO_CREDENTIAL &&
294 cred->name != GSS_C_NO_NAME) {
295 major = gss_duplicate_name(minor, cred->name, &ctx->acceptorName);
296 if (GSS_ERROR(major))
300 wpabuf_set(&respData, inputToken->value, inputToken->length);
301 ctx->acceptorCtx.eapPolInterface->eapRespData = &respData;
302 ctx->acceptorCtx.eapPolInterface->eapResp = TRUE;
304 code = eap_server_sm_step(ctx->acceptorCtx.eap);
306 if (ctx->acceptorCtx.eapPolInterface->eapReq) {
307 ctx->acceptorCtx.eapPolInterface->eapReq = 0;
308 major = GSS_S_CONTINUE_NEEDED;
311 if (ctx->acceptorCtx.eapPolInterface->eapSuccess) {
312 ctx->acceptorCtx.eapPolInterface->eapSuccess = 0;
313 major = acceptReady(minor, ctx);
314 if (GSS_ERROR(major))
317 ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
318 major = GSS_S_CONTINUE_NEEDED;
319 } else if (ctx->acceptorCtx.eapPolInterface->eapFail) {
320 ctx->acceptorCtx.eapPolInterface->eapFail = 0;
321 major = GSS_S_FAILURE;
322 } else if (code == 0) {
323 major = GSS_S_FAILURE;
326 if (ctx->acceptorCtx.eapPolInterface->eapReqData != NULL) {
329 buf.length = wpabuf_len(ctx->acceptorCtx.eapPolInterface->eapReqData);
330 buf.value = (void *)wpabuf_head(ctx->acceptorCtx.eapPolInterface->eapReqData);
332 tmpMajor = duplicateBuffer(&tmpMinor, &buf, outputToken);
333 if (GSS_ERROR(tmpMajor)) {
341 ctx->acceptorCtx.eapPolInterface->eapRespData = NULL;
347 eapGssSmAcceptGssChannelBindings(OM_uint32 *minor,
350 gss_buffer_t inputToken,
351 gss_channel_bindings_t chanBindings,
352 gss_buffer_t outputToken)
354 OM_uint32 major, tmpMinor;
355 gss_iov_buffer_desc iov[2];
357 outputToken->length = 0;
358 outputToken->value = NULL;
360 if (chanBindings == GSS_C_NO_CHANNEL_BINDINGS) {
361 ctx->state = EAP_STATE_ESTABLISHED;
362 return GSS_S_COMPLETE;
365 if (inputToken->length < 14) {
366 return GSS_S_DEFECTIVE_TOKEN;
369 iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
370 iov[0].buffer.length = 0;
371 iov[0].buffer.value = NULL;
373 major = gssEapEncodeGssChannelBindings(minor, chanBindings,
375 if (GSS_ERROR(major))
378 iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER;
379 iov[1].buffer.length = 16;
380 iov[1].buffer.value = (unsigned char *)inputToken->value - 2;
382 assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
384 iov[2].type = GSS_IOV_BUFFER_TYPE_TRAILER;
385 iov[2].buffer.length = inputToken->length - 14;
386 iov[2].buffer.value = (unsigned char *)inputToken->value + 14;
388 major = gssEapUnwrapOrVerifyMIC(minor, ctx, NULL, NULL,
389 iov, 3, TOK_TYPE_GSS_CB);
390 if (major == GSS_S_COMPLETE) {
391 ctx->state = EAP_STATE_ESTABLISHED;
394 gss_release_buffer(&tmpMinor, &iov[0].buffer);
400 eapGssSmAcceptEstablished(OM_uint32 *minor,
403 gss_buffer_t inputToken,
404 gss_channel_bindings_t chanBindings,
405 gss_buffer_t outputToken)
407 /* Called with already established context */
409 return GSS_S_BAD_STATUS;
412 static struct eap_gss_acceptor_sm {
413 enum gss_eap_token_type inputTokenType;
414 enum gss_eap_token_type outputTokenType;
415 OM_uint32 (*processToken)(OM_uint32 *,
419 gss_channel_bindings_t,
421 } eapGssAcceptorSm[] = {
422 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, eapGssSmAcceptAuthenticate },
424 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, NULL },
425 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, NULL },
427 { TOK_TYPE_GSS_CB, TOK_TYPE_NONE, eapGssSmAcceptGssChannelBindings },
428 { TOK_TYPE_NONE, TOK_TYPE_NONE, eapGssSmAcceptEstablished },
432 gss_accept_sec_context(OM_uint32 *minor,
433 gss_ctx_id_t *context_handle,
435 gss_buffer_t input_token,
436 gss_channel_bindings_t input_chan_bindings,
437 gss_name_t *src_name,
439 gss_buffer_t output_token,
440 OM_uint32 *ret_flags,
442 gss_cred_id_t *delegated_cred_handle)
445 OM_uint32 tmpMajor, tmpMinor;
446 gss_ctx_id_t ctx = *context_handle;
447 struct eap_gss_acceptor_sm *sm = NULL;
448 gss_buffer_desc innerInputToken, innerOutputToken;
452 innerOutputToken.length = 0;
453 innerOutputToken.value = NULL;
455 output_token->length = 0;
456 output_token->value = NULL;
458 if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) {
459 return GSS_S_NO_CRED;
462 if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
463 return GSS_S_DEFECTIVE_TOKEN;
466 if (ctx == GSS_C_NO_CONTEXT) {
467 major = gssEapAllocContext(minor, &ctx);
468 if (GSS_ERROR(major))
471 *context_handle = ctx;
474 GSSEAP_MUTEX_LOCK(&ctx->mutex);
476 sm = &eapGssAcceptorSm[ctx->state];
478 major = gssEapVerifyToken(minor, ctx, input_token,
479 sm->inputTokenType, &innerInputToken);
480 if (GSS_ERROR(major))
483 /* If credentials were provided, check they're usable with this mech */
484 if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
485 major = GSS_S_BAD_MECH;
490 sm = &eapGssAcceptorSm[ctx->state];
492 major = (sm->processToken)(minor,
498 if (GSS_ERROR(major))
500 } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.length == 0);
502 if (mech_type != NULL) {
503 if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type))
504 duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type);
506 if (innerOutputToken.length != 0) {
507 tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
508 sm->outputTokenType, output_token);
509 if (GSS_ERROR(tmpMajor)) {
515 if (ret_flags != NULL)
516 *ret_flags = ctx->gssFlags;
517 if (delegated_cred_handle != NULL)
518 *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
520 if (major == GSS_S_COMPLETE) {
521 if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
522 major = gss_duplicate_name(&tmpMinor, ctx->initiatorName, src_name);
523 if (GSS_ERROR(major))
526 if (time_rec != NULL)
527 gss_context_time(&tmpMinor, ctx, time_rec);
530 assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
533 GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
535 if (GSS_ERROR(major))
536 gssEapReleaseContext(&tmpMinor, context_handle);
538 gss_release_buffer(&tmpMinor, &innerOutputToken);