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"
38 #define EAP_MAX_METHODS 8
40 #define EAP_TTLS_AUTH_PAP 1
41 #define EAP_TTLS_AUTH_CHAP 2
42 #define EAP_TTLS_AUTH_MSCHAP 4
43 #define EAP_TTLS_AUTH_MSCHAPV2 8
49 } methods[EAP_MAX_METHODS];
52 int password_hash; /* whether password is hashed with
53 * nt_password_hash() */
56 int ttls_auth; /* bitfield of
57 * EAP_TTLS_AUTH_{PAP,CHAP,MSCHAP,MSCHAPV2} */
60 struct eap_eapol_interface {
61 /* Lower layer to full authenticator variables */
62 Boolean eapResp; /* shared with EAPOL Backend Authentication */
63 struct wpabuf *eapRespData;
66 Boolean eapRestart; /* shared with EAPOL Authenticator PAE */
70 /* Full authenticator to lower layer variables */
71 Boolean eapReq; /* shared with EAPOL Backend Authentication */
72 Boolean eapNoReq; /* shared with EAPOL Backend Authentication */
76 struct wpabuf *eapReqData;
79 Boolean eapKeyAvailable; /* called keyAvailable in IEEE 802.1X-2004 */
81 /* AAA interface to full authenticator variables */
86 struct wpabuf *aaaEapReqData;
88 size_t aaaEapKeyDataLen;
89 Boolean aaaEapKeyAvailable;
92 /* Full authenticator to AAA interface variables */
94 struct wpabuf *aaaEapRespData;
95 /* aaaIdentity -> eap_get_identity() */
99 #define eapol_callbacks SERVER_eapol_callbacks
101 struct eapol_callbacks {
102 int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
103 int phase2, struct eap_user *user);
104 const char * (*get_eap_req_id_text)(void *ctx, size_t *len);
107 #define eap_config SERVER_eap_config
112 void *eap_sim_db_priv;
113 Boolean backend_auth;
115 u8 *pac_opaque_encr_key;
117 size_t eap_fast_a_id_len;
118 char *eap_fast_a_id_info;
120 int pac_key_lifetime;
121 int pac_key_refresh_time;
122 int eap_sim_aka_result_ind;
124 struct wps_context *wps;
125 const struct wpabuf *assoc_wps_ie;
130 struct eap_sm * eap_server_sm_init(void *eapol_ctx,
131 struct eapol_callbacks *eapol_cb,
132 struct eap_config *eap_conf);
133 void eap_server_sm_deinit(struct eap_sm *sm);
134 int eap_server_sm_step(struct eap_sm *sm);
135 void eap_sm_notify_cached(struct eap_sm *sm);
136 void eap_sm_pending_cb(struct eap_sm *sm);
137 int eap_sm_method_pending(struct eap_sm *sm);
138 const u8 * eap_get_identity(struct eap_sm *sm, size_t *len);
139 struct eap_eapol_interface * eap_get_interface(struct eap_sm *sm);
141 #include <eap_server/eap_i.h>
144 initTls(OM_uint32 *minor,
147 struct tls_config tconf;
148 struct tls_connection_params tparams;
150 memset(&tconf, 0, sizeof(tconf));
151 ctx->acceptorCtx.tlsContext = tls_init(&tconf);
152 if (ctx->acceptorCtx.tlsContext == NULL)
153 return GSS_S_FAILURE;
155 memset(&tparams, 0, sizeof(tparams));
156 tparams.ca_cert = "ca.pem";
157 tparams.client_cert = "server.pem";
158 tparams.private_key = "server-key.pem";
160 if (tls_global_set_params(ctx->acceptorCtx.tlsContext, &tparams)) {
161 return GSS_S_FAILURE;
164 if (tls_global_set_verify(ctx->acceptorCtx.tlsContext, 0)) {
165 return GSS_S_FAILURE;
168 return GSS_S_COMPLETE;
172 serverGetEapUser(void *ctx,
173 const unsigned char *identity,
174 size_t identityLength,
176 struct eap_user *user)
178 gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx;
179 OM_uint32 major, minor;
182 memset(user, 0, sizeof(*user));
184 buf.length = identityLength;
185 buf.value = (void *)identity;
188 user->methods[0].vendor = EAP_VENDOR_IETF;
189 user->methods[0].method = EAP_TYPE_PEAP;
193 major = gssEapImportName(&minor, &buf, GSS_C_NT_USER_NAME,
194 &gssCtx->initiatorName);
195 if (GSS_ERROR(major))
199 * OK, obviously there is no real security here, this is simply
200 * for testing the token exchange; this code will be completely
201 * replaced with libradsec once that library is available.
203 user->methods[0].vendor = EAP_VENDOR_IETF;
204 user->methods[0].method = EAP_TYPE_MSCHAPV2;
205 user->password = (unsigned char *)strdup(" ");
206 user->password_len = 1;
212 serverGetEapReqIdText(void *ctx,
218 #endif /* BUILTIN_EAP */
221 acceptReady(OM_uint32 *minor, gss_ctx_id_t ctx)
225 /* Cache encryption type derived from selected mechanism OID */
226 major = gssEapOidToEnctype(minor, ctx->mechanismUsed, &ctx->encryptionType);
227 if (GSS_ERROR(major))
230 if (ctx->encryptionType != ENCTYPE_NULL &&
231 ctx->acceptorCtx.eapPolInterface->eapKeyAvailable) {
232 major = gssEapDeriveRfc3961Key(minor,
233 ctx->acceptorCtx.eapPolInterface->eapKeyData,
234 ctx->acceptorCtx.eapPolInterface->eapKeyDataLen,
237 if (GSS_ERROR(major))
240 major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
242 if (GSS_ERROR(major))
246 * draft-howlett-eap-gss says that integrity/confidentialty should
247 * always be advertised as available, but if we have no keying
248 * material it seems confusing to the caller to advertise this.
250 ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
253 sequenceInit(&ctx->seqState, ctx->recvSeq,
254 ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
255 ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
258 return GSS_S_COMPLETE;
262 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
265 gss_buffer_t inputToken,
266 gss_channel_bindings_t chanBindings,
267 gss_buffer_t outputToken)
270 OM_uint32 tmpMinor, tmpMajor;
272 struct wpabuf respData;
273 static struct eapol_callbacks cb = { serverGetEapUser, serverGetEapReqIdText };
274 if (ctx->acceptorCtx.eap == NULL) {
275 struct eap_config eapConfig;
277 major = initTls(minor, ctx);
278 if (GSS_ERROR(major))
281 memset(&eapConfig, 0, sizeof(eapConfig));
282 eapConfig.eap_server = 1;
283 eapConfig.ssl_ctx = ctx->acceptorCtx.tlsContext;
285 ctx->acceptorCtx.eap = eap_server_sm_init(ctx, &cb, &eapConfig);
286 if (ctx->acceptorCtx.eap == NULL) {
287 major = GSS_S_FAILURE;
291 ctx->acceptorCtx.eapPolInterface = eap_get_interface(ctx->acceptorCtx.eap);
292 ctx->acceptorCtx.eapPolInterface->portEnabled = TRUE;
293 ctx->acceptorCtx.eapPolInterface->eapRestart = TRUE;
296 if (ctx->acceptorName == GSS_C_NO_NAME &&
297 cred != GSS_C_NO_CREDENTIAL &&
298 cred->name != GSS_C_NO_NAME) {
299 major = gss_duplicate_name(minor, cred->name, &ctx->acceptorName);
300 if (GSS_ERROR(major))
304 wpabuf_set(&respData, inputToken->value, inputToken->length);
305 ctx->acceptorCtx.eapPolInterface->eapRespData = &respData;
306 ctx->acceptorCtx.eapPolInterface->eapResp = TRUE;
308 code = eap_server_sm_step(ctx->acceptorCtx.eap);
310 if (ctx->acceptorCtx.eapPolInterface->eapReq) {
311 ctx->acceptorCtx.eapPolInterface->eapReq = 0;
312 major = GSS_S_CONTINUE_NEEDED;
315 if (ctx->acceptorCtx.eapPolInterface->eapSuccess) {
316 ctx->acceptorCtx.eapPolInterface->eapSuccess = 0;
317 major = acceptReady(minor, ctx);
318 if (GSS_ERROR(major))
321 ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
322 major = GSS_S_CONTINUE_NEEDED;
323 } else if (ctx->acceptorCtx.eapPolInterface->eapFail) {
324 ctx->acceptorCtx.eapPolInterface->eapFail = 0;
325 major = GSS_S_FAILURE;
326 } else if (code == 0) {
327 major = GSS_S_FAILURE;
330 if (ctx->acceptorCtx.eapPolInterface->eapReqData != NULL) {
333 buf.length = wpabuf_len(ctx->acceptorCtx.eapPolInterface->eapReqData);
334 buf.value = (void *)wpabuf_head(ctx->acceptorCtx.eapPolInterface->eapReqData);
336 tmpMajor = duplicateBuffer(&tmpMinor, &buf, outputToken);
337 if (GSS_ERROR(tmpMajor)) {
345 ctx->acceptorCtx.eapPolInterface->eapRespData = NULL;
351 eapGssSmAcceptGssChannelBindings(OM_uint32 *minor,
354 gss_buffer_t inputToken,
355 gss_channel_bindings_t chanBindings,
356 gss_buffer_t outputToken)
358 OM_uint32 major, tmpMinor;
359 gss_iov_buffer_desc iov[2];
361 outputToken->length = 0;
362 outputToken->value = NULL;
364 if (chanBindings == GSS_C_NO_CHANNEL_BINDINGS) {
365 ctx->state = EAP_STATE_ESTABLISHED;
366 return GSS_S_COMPLETE;
369 if (inputToken->length < 14) {
370 return GSS_S_DEFECTIVE_TOKEN;
373 iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
374 iov[0].buffer.length = 0;
375 iov[0].buffer.value = NULL;
377 if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
378 iov[0].buffer = chanBindings->application_data;
380 iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER;
381 iov[1].buffer.length = 16;
382 iov[1].buffer.value = (unsigned char *)inputToken->value - 2;
384 assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
386 iov[2].type = GSS_IOV_BUFFER_TYPE_TRAILER;
387 iov[2].buffer.length = inputToken->length - 14;
388 iov[2].buffer.value = (unsigned char *)inputToken->value + 14;
390 major = gssEapUnwrapOrVerifyMIC(minor, ctx, NULL, NULL,
391 iov, 3, TOK_TYPE_GSS_CB);
392 if (major == GSS_S_COMPLETE) {
393 ctx->state = EAP_STATE_ESTABLISHED;
397 gss_release_buffer(&tmpMinor, &iov[0].buffer);
404 eapGssSmAcceptEstablished(OM_uint32 *minor,
407 gss_buffer_t inputToken,
408 gss_channel_bindings_t chanBindings,
409 gss_buffer_t outputToken)
411 /* Called with already established context */
413 return GSS_S_BAD_STATUS;
416 static struct eap_gss_acceptor_sm {
417 enum gss_eap_token_type inputTokenType;
418 enum gss_eap_token_type outputTokenType;
419 OM_uint32 (*processToken)(OM_uint32 *,
423 gss_channel_bindings_t,
425 } eapGssAcceptorSm[] = {
426 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, eapGssSmAcceptAuthenticate },
428 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, NULL },
429 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, NULL },
431 { TOK_TYPE_GSS_CB, TOK_TYPE_NONE, eapGssSmAcceptGssChannelBindings },
432 { TOK_TYPE_NONE, TOK_TYPE_NONE, eapGssSmAcceptEstablished },
436 gss_accept_sec_context(OM_uint32 *minor,
437 gss_ctx_id_t *context_handle,
439 gss_buffer_t input_token,
440 gss_channel_bindings_t input_chan_bindings,
441 gss_name_t *src_name,
443 gss_buffer_t output_token,
444 OM_uint32 *ret_flags,
446 gss_cred_id_t *delegated_cred_handle)
449 OM_uint32 tmpMajor, tmpMinor;
450 gss_ctx_id_t ctx = *context_handle;
451 struct eap_gss_acceptor_sm *sm = NULL;
452 gss_buffer_desc innerInputToken, innerOutputToken;
456 innerOutputToken.length = 0;
457 innerOutputToken.value = NULL;
459 output_token->length = 0;
460 output_token->value = NULL;
462 if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) {
463 return GSS_S_NO_CRED;
466 if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
467 return GSS_S_DEFECTIVE_TOKEN;
470 if (ctx == GSS_C_NO_CONTEXT) {
471 major = gssEapAllocContext(minor, &ctx);
472 if (GSS_ERROR(major))
475 *context_handle = ctx;
478 GSSEAP_MUTEX_LOCK(&ctx->mutex);
480 sm = &eapGssAcceptorSm[ctx->state];
482 major = gssEapVerifyToken(minor, ctx, input_token,
483 sm->inputTokenType, &innerInputToken);
484 if (GSS_ERROR(major))
487 /* If credentials were provided, check they're usable with this mech */
488 if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
489 major = GSS_S_BAD_MECH;
494 sm = &eapGssAcceptorSm[ctx->state];
496 major = (sm->processToken)(minor,
502 if (GSS_ERROR(major))
504 } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.length == 0);
506 if (mech_type != NULL) {
507 if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type))
508 duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type);
510 if (innerOutputToken.length != 0) {
511 tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
512 sm->outputTokenType, output_token);
513 if (GSS_ERROR(tmpMajor)) {
519 if (ret_flags != NULL)
520 *ret_flags = ctx->gssFlags;
521 if (delegated_cred_handle != NULL)
522 *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
524 if (major == GSS_S_COMPLETE) {
525 if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
526 major = gss_duplicate_name(&tmpMinor, ctx->initiatorName, src_name);
527 if (GSS_ERROR(major))
530 if (time_rec != NULL)
531 gss_context_time(&tmpMinor, ctx, time_rec);
534 assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
537 GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
539 if (GSS_ERROR(major))
540 gssEapReleaseContext(&tmpMinor, context_handle);
542 gss_release_buffer(&tmpMinor, &innerOutputToken);