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"
36 #define EAP_KEY_AVAILABLE(ctx) ((ctx)->acceptorCtx.eapPolInterface->eapKeyAvailable)
37 #define EAP_KEY_DATA(ctx) ((ctx)->acceptorCtx.eapPolInterface->eapKeyData)
38 #define EAP_KEY_LENGTH(ctx) ((ctx)->acceptorCtx.eapPolInterface->eapKeyDataLen)
40 #define EAP_KEY_AVAILABLE(ctx) 0
41 #define EAP_KEY_DATA(ctx) NULL
42 #define EAP_KEY_LENGTH(ctx) 0
43 #endif /* BUILTIN_EAP */
46 acceptReady(OM_uint32 *minor, gss_ctx_id_t ctx);
49 #define EAP_MAX_METHODS 8
51 #define EAP_TTLS_AUTH_PAP 1
52 #define EAP_TTLS_AUTH_CHAP 2
53 #define EAP_TTLS_AUTH_MSCHAP 4
54 #define EAP_TTLS_AUTH_MSCHAPV2 8
60 } methods[EAP_MAX_METHODS];
63 int password_hash; /* whether password is hashed with
64 * nt_password_hash() */
67 int ttls_auth; /* bitfield of
68 * EAP_TTLS_AUTH_{PAP,CHAP,MSCHAP,MSCHAPV2} */
71 struct eap_eapol_interface {
72 /* Lower layer to full authenticator variables */
73 Boolean eapResp; /* shared with EAPOL Backend Authentication */
74 struct wpabuf *eapRespData;
77 Boolean eapRestart; /* shared with EAPOL Authenticator PAE */
81 /* Full authenticator to lower layer variables */
82 Boolean eapReq; /* shared with EAPOL Backend Authentication */
83 Boolean eapNoReq; /* shared with EAPOL Backend Authentication */
87 struct wpabuf *eapReqData;
90 Boolean eapKeyAvailable; /* called keyAvailable in IEEE 802.1X-2004 */
92 /* AAA interface to full authenticator variables */
97 struct wpabuf *aaaEapReqData;
99 size_t aaaEapKeyDataLen;
100 Boolean aaaEapKeyAvailable;
101 int aaaMethodTimeout;
103 /* Full authenticator to AAA interface variables */
105 struct wpabuf *aaaEapRespData;
106 /* aaaIdentity -> eap_get_identity() */
110 #define eapol_callbacks SERVER_eapol_callbacks
112 struct eapol_callbacks {
113 int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
114 int phase2, struct eap_user *user);
115 const char * (*get_eap_req_id_text)(void *ctx, size_t *len);
118 #define eap_config SERVER_eap_config
123 void *eap_sim_db_priv;
124 Boolean backend_auth;
126 u8 *pac_opaque_encr_key;
128 size_t eap_fast_a_id_len;
129 char *eap_fast_a_id_info;
131 int pac_key_lifetime;
132 int pac_key_refresh_time;
133 int eap_sim_aka_result_ind;
135 struct wps_context *wps;
136 const struct wpabuf *assoc_wps_ie;
141 struct eap_sm * eap_server_sm_init(void *eapol_ctx,
142 struct eapol_callbacks *eapol_cb,
143 struct eap_config *eap_conf);
144 void eap_server_sm_deinit(struct eap_sm *sm);
145 int eap_server_sm_step(struct eap_sm *sm);
146 void eap_sm_notify_cached(struct eap_sm *sm);
147 void eap_sm_pending_cb(struct eap_sm *sm);
148 int eap_sm_method_pending(struct eap_sm *sm);
149 const u8 * eap_get_identity(struct eap_sm *sm, size_t *len);
150 struct eap_eapol_interface * eap_get_interface(struct eap_sm *sm);
152 #include <eap_server/eap_i.h>
155 initTls(OM_uint32 *minor,
158 struct tls_config tconf;
159 struct tls_connection_params tparams;
161 memset(&tconf, 0, sizeof(tconf));
162 ctx->acceptorCtx.tlsContext = tls_init(&tconf);
163 if (ctx->acceptorCtx.tlsContext == NULL)
164 return GSS_S_FAILURE;
166 memset(&tparams, 0, sizeof(tparams));
167 tparams.ca_cert = "ca.pem";
168 tparams.client_cert = "server.pem";
169 tparams.private_key = "server-key.pem";
171 if (tls_global_set_params(ctx->acceptorCtx.tlsContext, &tparams)) {
172 return GSS_S_FAILURE;
175 if (tls_global_set_verify(ctx->acceptorCtx.tlsContext, 0)) {
176 return GSS_S_FAILURE;
179 return GSS_S_COMPLETE;
183 serverGetEapUser(void *ctx,
184 const unsigned char *identity,
185 size_t identityLength,
187 struct eap_user *user)
189 gss_ctx_id_t gssCtx = (gss_ctx_id_t)ctx;
190 OM_uint32 major, minor;
193 memset(user, 0, sizeof(*user));
195 buf.length = identityLength;
196 buf.value = (void *)identity;
199 user->methods[0].vendor = EAP_VENDOR_IETF;
200 user->methods[0].method = EAP_TYPE_PEAP;
204 major = gssEapImportName(&minor, &buf, GSS_C_NT_USER_NAME,
205 &gssCtx->initiatorName);
206 if (GSS_ERROR(major))
210 * OK, obviously there is no real security here, this is simply
211 * for testing the token exchange; this code will be completely
212 * replaced with libradius once that library is available.
214 user->methods[0].vendor = EAP_VENDOR_IETF;
215 user->methods[0].method = EAP_TYPE_MSCHAPV2;
216 user->password = (unsigned char *)strdup(" ");
217 user->password_len = 1;
219 gssCtx->initiatorName->attrCtx = gssEapCreateAttrContext(NULL, gssCtx);
220 if (gssCtx->initiatorName->attrCtx != NULL)
221 gssCtx->initiatorName->flags |= NAME_FLAG_COMPOSITE;
227 serverGetEapReqIdText(void *ctx,
235 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
238 gss_buffer_t inputToken,
239 gss_channel_bindings_t chanBindings,
240 gss_buffer_t outputToken)
243 OM_uint32 tmpMinor, tmpMajor;
245 struct wpabuf respData;
246 static struct eapol_callbacks cb = { serverGetEapUser, serverGetEapReqIdText };
247 if (ctx->acceptorCtx.eap == NULL) {
248 struct eap_config eapConfig;
250 major = initTls(minor, ctx);
251 if (GSS_ERROR(major))
254 memset(&eapConfig, 0, sizeof(eapConfig));
255 eapConfig.eap_server = 1;
256 eapConfig.ssl_ctx = ctx->acceptorCtx.tlsContext;
258 ctx->acceptorCtx.eap = eap_server_sm_init(ctx, &cb, &eapConfig);
259 if (ctx->acceptorCtx.eap == NULL) {
260 major = GSS_S_FAILURE;
264 ctx->acceptorCtx.eapPolInterface = eap_get_interface(ctx->acceptorCtx.eap);
265 ctx->acceptorCtx.eapPolInterface->portEnabled = TRUE;
266 ctx->acceptorCtx.eapPolInterface->eapRestart = TRUE;
269 if (ctx->acceptorName == GSS_C_NO_NAME &&
270 cred != GSS_C_NO_CREDENTIAL &&
271 cred->name != GSS_C_NO_NAME) {
272 major = gss_duplicate_name(minor, cred->name, &ctx->acceptorName);
273 if (GSS_ERROR(major))
277 wpabuf_set(&respData, inputToken->value, inputToken->length);
278 ctx->acceptorCtx.eapPolInterface->eapRespData = &respData;
279 ctx->acceptorCtx.eapPolInterface->eapResp = TRUE;
281 code = eap_server_sm_step(ctx->acceptorCtx.eap);
283 if (ctx->acceptorCtx.eapPolInterface->eapReq) {
284 ctx->acceptorCtx.eapPolInterface->eapReq = 0;
285 major = GSS_S_CONTINUE_NEEDED;
288 if (ctx->acceptorCtx.eapPolInterface->eapSuccess) {
289 ctx->acceptorCtx.eapPolInterface->eapSuccess = 0;
290 major = acceptReady(minor, ctx);
291 if (GSS_ERROR(major))
294 ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
295 major = GSS_S_CONTINUE_NEEDED;
296 } else if (ctx->acceptorCtx.eapPolInterface->eapFail) {
297 ctx->acceptorCtx.eapPolInterface->eapFail = 0;
298 major = GSS_S_FAILURE;
299 } else if (code == 0) {
300 major = GSS_S_FAILURE;
303 if (ctx->acceptorCtx.eapPolInterface->eapReqData != NULL) {
306 buf.length = wpabuf_len(ctx->acceptorCtx.eapPolInterface->eapReqData);
307 buf.value = (void *)wpabuf_head(ctx->acceptorCtx.eapPolInterface->eapReqData);
309 tmpMajor = duplicateBuffer(&tmpMinor, &buf, outputToken);
310 if (GSS_ERROR(tmpMajor)) {
318 ctx->acceptorCtx.eapPolInterface->eapRespData = NULL;
324 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
327 gss_buffer_t inputToken,
328 gss_channel_bindings_t chanBindings,
329 gss_buffer_t outputToken)
331 OM_uint32 major, tmpMinor;
336 #endif /* BUILTIN_EAP */
339 eapGssSmAcceptGssChannelBindings(OM_uint32 *minor,
342 gss_buffer_t inputToken,
343 gss_channel_bindings_t chanBindings,
344 gss_buffer_t outputToken)
346 OM_uint32 major, tmpMinor;
347 gss_iov_buffer_desc iov[2];
349 outputToken->length = 0;
350 outputToken->value = NULL;
352 if (chanBindings == GSS_C_NO_CHANNEL_BINDINGS) {
353 ctx->state = EAP_STATE_ESTABLISHED;
354 return GSS_S_COMPLETE;
357 if (inputToken->length < 14) {
358 return GSS_S_DEFECTIVE_TOKEN;
361 iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
362 iov[0].buffer.length = 0;
363 iov[0].buffer.value = NULL;
365 if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
366 iov[0].buffer = chanBindings->application_data;
368 iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER;
369 iov[1].buffer.length = 16;
370 iov[1].buffer.value = (unsigned char *)inputToken->value - 2;
372 assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
374 iov[2].type = GSS_IOV_BUFFER_TYPE_TRAILER;
375 iov[2].buffer.length = inputToken->length - 14;
376 iov[2].buffer.value = (unsigned char *)inputToken->value + 14;
378 major = gssEapUnwrapOrVerifyMIC(minor, ctx, NULL, NULL,
379 iov, 3, TOK_TYPE_GSS_CB);
380 if (major == GSS_S_COMPLETE) {
381 ctx->state = EAP_STATE_ESTABLISHED;
385 gss_release_buffer(&tmpMinor, &iov[0].buffer);
392 eapGssSmAcceptEstablished(OM_uint32 *minor,
395 gss_buffer_t inputToken,
396 gss_channel_bindings_t chanBindings,
397 gss_buffer_t outputToken)
399 /* Called with already established context */
401 return GSS_S_BAD_STATUS;
404 static struct gss_eap_acceptor_sm {
405 enum gss_eap_token_type inputTokenType;
406 enum gss_eap_token_type outputTokenType;
407 OM_uint32 (*processToken)(OM_uint32 *,
411 gss_channel_bindings_t,
413 } eapGssAcceptorSm[] = {
414 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, eapGssSmAcceptAuthenticate },
416 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, NULL },
417 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, NULL },
419 { TOK_TYPE_GSS_CB, TOK_TYPE_NONE, eapGssSmAcceptGssChannelBindings },
420 { TOK_TYPE_NONE, TOK_TYPE_NONE, eapGssSmAcceptEstablished },
424 gss_accept_sec_context(OM_uint32 *minor,
425 gss_ctx_id_t *context_handle,
427 gss_buffer_t input_token,
428 gss_channel_bindings_t input_chan_bindings,
429 gss_name_t *src_name,
431 gss_buffer_t output_token,
432 OM_uint32 *ret_flags,
434 gss_cred_id_t *delegated_cred_handle)
437 OM_uint32 tmpMajor, tmpMinor;
438 gss_ctx_id_t ctx = *context_handle;
439 struct gss_eap_acceptor_sm *sm = NULL;
440 gss_buffer_desc innerInputToken = GSS_C_EMPTY_BUFFER;
441 gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
445 output_token->length = 0;
446 output_token->value = NULL;
448 if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) {
449 return GSS_S_NO_CRED;
452 if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
453 return GSS_S_DEFECTIVE_TOKEN;
456 if (ctx == GSS_C_NO_CONTEXT) {
457 major = gssEapAllocContext(minor, &ctx);
458 if (GSS_ERROR(major))
461 *context_handle = ctx;
464 GSSEAP_MUTEX_LOCK(&ctx->mutex);
466 sm = &eapGssAcceptorSm[ctx->state];
468 major = gssEapVerifyToken(minor, ctx, input_token,
469 sm->inputTokenType, &innerInputToken);
470 if (GSS_ERROR(major))
473 /* If credentials were provided, check they're usable with this mech */
474 if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
475 major = GSS_S_BAD_MECH;
480 sm = &eapGssAcceptorSm[ctx->state];
482 major = (sm->processToken)(minor,
488 if (GSS_ERROR(major))
490 } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.length == 0);
492 if (mech_type != NULL) {
493 if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type))
494 duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type);
496 if (innerOutputToken.length != 0) {
497 tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
498 sm->outputTokenType, output_token);
499 if (GSS_ERROR(tmpMajor)) {
505 if (ret_flags != NULL)
506 *ret_flags = ctx->gssFlags;
507 if (delegated_cred_handle != NULL)
508 *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
510 if (major == GSS_S_COMPLETE) {
511 if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
512 major = gss_duplicate_name(&tmpMinor, ctx->initiatorName, src_name);
513 if (GSS_ERROR(major))
516 if (time_rec != NULL)
517 gss_context_time(&tmpMinor, ctx, time_rec);
520 assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
523 GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
525 if (GSS_ERROR(major))
526 gssEapReleaseContext(&tmpMinor, context_handle);
528 gss_release_buffer(&tmpMinor, &innerOutputToken);
534 * Mark a context as ready for cryptographic operations
537 acceptReady(OM_uint32 *minor, gss_ctx_id_t ctx)
541 /* Cache encryption type derived from selected mechanism OID */
542 major = gssEapOidToEnctype(minor, ctx->mechanismUsed, &ctx->encryptionType);
543 if (GSS_ERROR(major))
546 if (ctx->encryptionType != ENCTYPE_NULL &&
547 EAP_KEY_AVAILABLE(ctx)) {
548 major = gssEapDeriveRfc3961Key(minor,
553 if (GSS_ERROR(major))
556 major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
558 if (GSS_ERROR(major))
562 * draft-howlett-eap-gss says that integrity/confidentialty should
563 * always be advertised as available, but if we have no keying
564 * material it seems confusing to the caller to advertise this.
566 ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
569 major = sequenceInit(minor,
570 &ctx->seqState, ctx->recvSeq,
571 ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
572 ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
574 if (GSS_ERROR(major))
577 return GSS_S_COMPLETE;