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 RC_CONFIG_FILE SYSCONFDIR "/radiusclient/radiusclient.conf"
38 * Mark a context as ready for cryptographic operations
41 acceptReady(OM_uint32 *minor, gss_ctx_id_t ctx)
45 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
47 /* Cache encryption type derived from selected mechanism OID */
48 major = gssEapOidToEnctype(minor, ctx->mechanismUsed,
49 &ctx->encryptionType);
53 vp = rc_avpair_get(ctx->acceptorCtx.avps, PW_USER_NAME, 0);
55 nameBuf.length = vp->lvalue;
56 nameBuf.value = vp->strvalue;
58 ctx->gssFlags |= GSS_C_ANON_FLAG;
61 major = gssEapImportName(minor, &nameBuf, GSS_C_NT_USER_NAME,
66 vp = rc_avpair_get(ctx->acceptorCtx.avps, PW_MSCHAP2_SUCCESS, 0);
67 if (ctx->encryptionType != ENCTYPE_NULL && vp != NULL) {
68 major = gssEapDeriveRfc3961Key(minor,
69 (unsigned char *)vp->strvalue,
76 major = rfc3961ChecksumTypeForKey(minor, &ctx->rfc3961Key,
82 * draft-howlett-eap-gss says that integrity/confidentialty should
83 * always be advertised as available, but if we have no keying
84 * material it seems confusing to the caller to advertise this.
86 ctx->gssFlags &= ~(GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
89 major = sequenceInit(minor,
90 &ctx->seqState, ctx->recvSeq,
91 ((ctx->gssFlags & GSS_C_REPLAY_FLAG) != 0),
92 ((ctx->gssFlags & GSS_C_SEQUENCE_FLAG) != 0),
97 return GSS_S_COMPLETE;
101 eapGssSmAcceptIdentity(OM_uint32 *minor,
104 gss_buffer_t inputToken,
105 gss_channel_bindings_t chanBindings,
106 gss_buffer_t outputToken)
112 unsigned char data[5];
114 gss_buffer_desc pduBuffer;
115 char *config = RC_CONFIG_FILE;
117 if (inputToken != GSS_C_NO_BUFFER && inputToken->length != 0)
118 return GSS_S_DEFECTIVE_TOKEN;
120 assert(ctx->acceptorCtx.radHandle == NULL);
122 if (cred != GSS_C_NO_CREDENTIAL && cred->radiusConfigFile != NULL)
123 config = cred->radiusConfigFile;
125 rh = ctx->acceptorCtx.radHandle = rc_read_config(config);
128 return GSS_S_FAILURE;
131 if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary")) != 0) {
133 return GSS_S_FAILURE;
136 if (ctx->acceptorName == GSS_C_NO_NAME &&
137 cred != GSS_C_NO_CREDENTIAL &&
138 cred->name != GSS_C_NO_NAME) {
139 major = gss_duplicate_name(minor, cred->name, &ctx->acceptorName);
140 if (GSS_ERROR(major))
144 pdu.eap.code = EAP_CODE_REQUEST;
145 pdu.eap.identifier = 0;
146 pdu.eap.length = htons(sizeof(pdu.data));
147 pdu.data[4] = EAP_TYPE_IDENTITY;
149 pduBuffer.length = sizeof(pdu.data);
150 pduBuffer.value = pdu.data;
152 major = duplicateBuffer(minor, &pduBuffer, outputToken);
153 if (GSS_ERROR(major))
156 ctx->state = EAP_STATE_AUTHENTICATE;
158 return GSS_S_CONTINUE_NEEDED;
162 eapGssSmAcceptAuthenticate(OM_uint32 *minor,
165 gss_buffer_t inputToken,
166 gss_channel_bindings_t chanBindings,
167 gss_buffer_t outputToken)
170 OM_uint32 service = PW_AUTHENTICATE_ONLY;
172 VALUE_PAIR *send = NULL;
173 VALUE_PAIR *received = NULL;
174 rc_handle *rh = ctx->acceptorCtx.radHandle;
175 char msgBuffer[4096];
177 if (rc_avpair_add(rh, &send, PW_EAP_MESSAGE,
178 inputToken->value, inputToken->length, 0) == NULL) {
180 major = GSS_S_FAILURE;
184 if (rc_avpair_add(rh, &send, PW_SERVICE_TYPE, &service, -1, 0) == NULL) {
186 major = GSS_S_FAILURE;
190 code = rc_auth(rh, 0, send, &received, msgBuffer);
193 major = GSS_S_UNAVAILABLE;
197 if (code == OK_RC || code == PW_ACCESS_CHALLENGE) {
198 VALUE_PAIR *eapResponse;
199 gss_buffer_desc eapBuf = GSS_C_EMPTY_BUFFER;
201 eapResponse = rc_avpair_get(received, PW_EAP_MESSAGE, 0);
202 if (eapResponse != NULL) {
203 eapBuf.length = eapResponse->lvalue;
204 eapBuf.value = eapResponse->strvalue;
207 major = duplicateBuffer(minor, &eapBuf, outputToken);
208 if (GSS_ERROR(major))
211 major = GSS_S_CONTINUE_NEEDED;
213 major = GSS_S_FAILURE;
218 ctx->acceptorCtx.avps = received;
221 major = acceptReady(minor, ctx);
222 if (GSS_ERROR(major))
225 ctx->state = EAP_STATE_GSS_CHANNEL_BINDINGS;
226 major = GSS_S_CONTINUE_NEEDED;
230 if (received != NULL)
231 rc_avpair_free(received);
237 eapGssSmAcceptGssChannelBindings(OM_uint32 *minor,
240 gss_buffer_t inputToken,
241 gss_channel_bindings_t chanBindings,
242 gss_buffer_t outputToken)
245 gss_iov_buffer_desc iov[2];
247 outputToken->length = 0;
248 outputToken->value = NULL;
250 if (chanBindings == GSS_C_NO_CHANNEL_BINDINGS) {
251 ctx->state = EAP_STATE_ESTABLISHED;
252 return GSS_S_COMPLETE;
255 if (inputToken->length < 14) {
256 return GSS_S_DEFECTIVE_TOKEN;
259 iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
260 iov[0].buffer.length = 0;
261 iov[0].buffer.value = NULL;
263 if (chanBindings != GSS_C_NO_CHANNEL_BINDINGS)
264 iov[0].buffer = chanBindings->application_data;
266 iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER;
267 iov[1].buffer.length = 16;
268 iov[1].buffer.value = (unsigned char *)inputToken->value - 2;
270 assert(load_uint16_be(iov[1].buffer.value) == TOK_TYPE_GSS_CB);
272 iov[2].type = GSS_IOV_BUFFER_TYPE_TRAILER;
273 iov[2].buffer.length = inputToken->length - 14;
274 iov[2].buffer.value = (unsigned char *)inputToken->value + 14;
276 major = gssEapUnwrapOrVerifyMIC(minor, ctx, NULL, NULL,
277 iov, 3, TOK_TYPE_GSS_CB);
278 if (major == GSS_S_COMPLETE) {
279 ctx->state = EAP_STATE_ESTABLISHED;
283 gss_release_buffer(&tmpMinor, &iov[0].buffer);
290 eapGssSmAcceptEstablished(OM_uint32 *minor,
293 gss_buffer_t inputToken,
294 gss_channel_bindings_t chanBindings,
295 gss_buffer_t outputToken)
297 /* Called with already established context */
299 return GSS_S_BAD_STATUS;
302 static struct gss_eap_acceptor_sm {
303 enum gss_eap_token_type inputTokenType;
304 enum gss_eap_token_type outputTokenType;
305 OM_uint32 (*processToken)(OM_uint32 *,
309 gss_channel_bindings_t,
311 } eapGssAcceptorSm[] = {
312 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, eapGssSmAcceptIdentity },
313 { TOK_TYPE_EAP_RESP, TOK_TYPE_EAP_REQ, eapGssSmAcceptAuthenticate },
314 { TOK_TYPE_GSS_CB, TOK_TYPE_NONE, eapGssSmAcceptGssChannelBindings },
315 { TOK_TYPE_NONE, TOK_TYPE_NONE, eapGssSmAcceptEstablished },
319 gss_accept_sec_context(OM_uint32 *minor,
320 gss_ctx_id_t *context_handle,
322 gss_buffer_t input_token,
323 gss_channel_bindings_t input_chan_bindings,
324 gss_name_t *src_name,
326 gss_buffer_t output_token,
327 OM_uint32 *ret_flags,
329 gss_cred_id_t *delegated_cred_handle)
332 OM_uint32 tmpMajor, tmpMinor;
333 gss_ctx_id_t ctx = *context_handle;
334 struct gss_eap_acceptor_sm *sm = NULL;
335 gss_buffer_desc innerInputToken = GSS_C_EMPTY_BUFFER;
336 gss_buffer_desc innerOutputToken = GSS_C_EMPTY_BUFFER;
340 output_token->length = 0;
341 output_token->value = NULL;
343 if (cred != GSS_C_NO_CREDENTIAL && !(cred->flags & CRED_FLAG_ACCEPT)) {
344 return GSS_S_NO_CRED;
347 if (input_token == GSS_C_NO_BUFFER || input_token->length == 0) {
348 return GSS_S_DEFECTIVE_TOKEN;
351 if (ctx == GSS_C_NO_CONTEXT) {
352 major = gssEapAllocContext(minor, &ctx);
353 if (GSS_ERROR(major))
356 *context_handle = ctx;
359 GSSEAP_MUTEX_LOCK(&ctx->mutex);
361 sm = &eapGssAcceptorSm[ctx->state];
363 major = gssEapVerifyToken(minor, ctx, input_token,
364 sm->inputTokenType, &innerInputToken);
365 if (GSS_ERROR(major))
368 /* If credentials were provided, check they're usable with this mech */
369 if (!gssEapCredAvailable(cred, ctx->mechanismUsed)) {
370 major = GSS_S_BAD_MECH;
375 sm = &eapGssAcceptorSm[ctx->state];
377 major = (sm->processToken)(minor,
383 if (GSS_ERROR(major))
385 } while (major == GSS_S_CONTINUE_NEEDED && innerOutputToken.length == 0);
387 if (mech_type != NULL) {
388 if (!gssEapInternalizeOid(ctx->mechanismUsed, mech_type))
389 duplicateOid(&tmpMinor, ctx->mechanismUsed, mech_type);
391 if (innerOutputToken.length != 0) {
392 tmpMajor = gssEapMakeToken(&tmpMinor, ctx, &innerOutputToken,
393 sm->outputTokenType, output_token);
394 if (GSS_ERROR(tmpMajor)) {
400 if (ret_flags != NULL)
401 *ret_flags = ctx->gssFlags;
402 if (delegated_cred_handle != NULL)
403 *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
405 if (major == GSS_S_COMPLETE) {
406 if (src_name != NULL && ctx->initiatorName != GSS_C_NO_NAME) {
407 major = gss_duplicate_name(&tmpMinor, ctx->initiatorName, src_name);
408 if (GSS_ERROR(major))
411 if (time_rec != NULL)
412 gss_context_time(&tmpMinor, ctx, time_rec);
415 assert(ctx->state == EAP_STATE_ESTABLISHED || major == GSS_S_CONTINUE_NEEDED);
418 GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
420 if (GSS_ERROR(major))
421 gssEapReleaseContext(&tmpMinor, context_handle);
423 gss_release_buffer(&tmpMinor, &innerOutputToken);