gss_accept_sec_context.c revision 178692
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 2005 Doug Rabson
31556Srgrimes * All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes *
141556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241556Srgrimes * SUCH DAMAGE.
251556Srgrimes *
261556Srgrimes *	$FreeBSD: head/lib/libgssapi/gss_accept_sec_context.c 178692 2008-04-30 11:29:22Z dfr $
271556Srgrimes */
281556Srgrimes
291556Srgrimes#include <gssapi/gssapi.h>
301556Srgrimes#include <stdlib.h>
311556Srgrimes#include <string.h>
321556Srgrimes#include <errno.h>
331556Srgrimes
341556Srgrimes#include "mech_switch.h"
3536149Scharnier#include "context.h"
36105269Smarkm#include "cred.h"
3736149Scharnier#include "name.h"
381556Srgrimes
3999110SobrienOM_uint32 gss_accept_sec_context(OM_uint32 *minor_status,
4099110Sobrien    gss_ctx_id_t *context_handle,
411556Srgrimes    const gss_cred_id_t acceptor_cred_handle,
421556Srgrimes    const gss_buffer_t input_token,
431556Srgrimes    const gss_channel_bindings_t input_chan_bindings,
441556Srgrimes    gss_name_t *src_name,
451556Srgrimes    gss_OID *mech_type,
461556Srgrimes    gss_buffer_t output_token,
471556Srgrimes    OM_uint32 *ret_flags,
481556Srgrimes    OM_uint32 *time_rec,
491556Srgrimes    gss_cred_id_t *delegated_cred_handle)
501556Srgrimes{
511556Srgrimes	OM_uint32 major_status, mech_ret_flags;
521556Srgrimes	struct _gss_mech_switch *m;
531556Srgrimes	struct _gss_context *ctx = (struct _gss_context *) *context_handle;
541556Srgrimes	struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;
551556Srgrimes	struct _gss_mechanism_cred *mc;
561556Srgrimes	gss_cred_id_t acceptor_mc, delegated_mc;
571556Srgrimes	gss_name_t src_mn;
581556Srgrimes	int allocated_ctx;
5990110Simp
601556Srgrimes	*minor_status = 0;
611556Srgrimes	if (src_name) *src_name = 0;
621556Srgrimes	if (mech_type) *mech_type = 0;
631556Srgrimes	if (ret_flags) *ret_flags = 0;
641556Srgrimes	if (time_rec) *time_rec = 0;
651556Srgrimes	if (delegated_cred_handle) *delegated_cred_handle = 0;
661556Srgrimes	output_token->length = 0;
671556Srgrimes	output_token->value = 0;
681556Srgrimes
691556Srgrimes	/*
701556Srgrimes	 * If this is the first call (*context_handle is NULL), we must
711556Srgrimes	 * parse the input token to figure out the mechanism to use.
721556Srgrimes	 */
731556Srgrimes	if (*context_handle == GSS_C_NO_CONTEXT) {
7490110Simp		unsigned char *p = input_token->value;
751556Srgrimes		size_t len = input_token->length;
761556Srgrimes		size_t a, b;
771556Srgrimes		gss_OID_desc mech_oid;
781556Srgrimes
791556Srgrimes		/*
801556Srgrimes		 * Token must start with [APPLICATION 0] SEQUENCE.
811556Srgrimes		 */
821556Srgrimes		if (len == 0 || *p != 0x60)
831556Srgrimes			return (GSS_S_DEFECTIVE_TOKEN);
841556Srgrimes		p++;
851556Srgrimes		len--;
861556Srgrimes
871556Srgrimes		/*
8890110Simp		 * Decode the length and make sure it agrees with the
891556Srgrimes		 * token length.
901556Srgrimes		 */
911556Srgrimes		if (len == 0)
921556Srgrimes			return (GSS_S_DEFECTIVE_TOKEN);
931556Srgrimes		if ((*p & 0x80) == 0) {
941556Srgrimes			a = *p;
951556Srgrimes			p++;
961556Srgrimes			len--;
971556Srgrimes		} else {
9880818Sobrien			b = *p & 0x7f;
991556Srgrimes			p++;
1001556Srgrimes			len--;
1011556Srgrimes			if (len < b)
1021556Srgrimes				return (GSS_S_DEFECTIVE_TOKEN);
1031556Srgrimes			a = 0;
1041556Srgrimes			while (b) {
1051556Srgrimes				a = (a << 8) | *p;
1061556Srgrimes				p++;
1071556Srgrimes				len--;
10890110Simp				b--;
1091556Srgrimes			}
1101556Srgrimes		}
1117165Sjoerg		if (a != len)
1121556Srgrimes			return (GSS_S_DEFECTIVE_TOKEN);
1131556Srgrimes
1141556Srgrimes		/*
1151556Srgrimes		 * Decode the OID for the mechanism. Simplify life by
1161556Srgrimes		 * assuming that the OID length is less than 128 bytes.
1171556Srgrimes		 */
1188855Srgrimes		if (len < 2 || *p != 0x06)
1191556Srgrimes			return (GSS_S_DEFECTIVE_TOKEN);
1201556Srgrimes		if ((p[1] & 0x80) || p[1] > (len - 2))
12179452Sbrian			return (GSS_S_DEFECTIVE_TOKEN);
1221556Srgrimes		mech_oid.length = p[1];
1231556Srgrimes		p += 2;
1241556Srgrimes		len -= 2;
1251556Srgrimes		mech_oid.elements = p;
1261556Srgrimes
1271556Srgrimes		/*
1281556Srgrimes		 * Now that we have a mechanism, we can find the
1291556Srgrimes		 * implementation.
1301556Srgrimes		 */
1311556Srgrimes		ctx = malloc(sizeof(struct _gss_context));
1321556Srgrimes		if (!ctx) {
1331556Srgrimes			*minor_status = ENOMEM;
13490110Simp			return (GSS_S_DEFECTIVE_TOKEN);
1351556Srgrimes		}
1361556Srgrimes		memset(ctx, 0, sizeof(struct _gss_context));
1371556Srgrimes		m = ctx->gc_mech = _gss_find_mech_switch(&mech_oid);
1381556Srgrimes		if (!m) {
1391556Srgrimes			free(ctx);
1401556Srgrimes			return (GSS_S_BAD_MECH);
1411556Srgrimes		}
1421556Srgrimes		allocated_ctx = 1;
1431556Srgrimes	} else {
1441556Srgrimes		m = ctx->gc_mech;
1451556Srgrimes		allocated_ctx = 0;
1461556Srgrimes	}
1471556Srgrimes
1481556Srgrimes	if (cred) {
1491556Srgrimes		SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
1501556Srgrimes			if (mc->gmc_mech == m)
1511556Srgrimes				break;
1521556Srgrimes		if (!mc)
1531556Srgrimes			return (GSS_S_BAD_MECH);
1541556Srgrimes		acceptor_mc = mc->gmc_cred;
1551556Srgrimes	} else {
1561556Srgrimes		acceptor_mc = GSS_C_NO_CREDENTIAL;
1571556Srgrimes	}
158104130Sjmallett	delegated_mc = GSS_C_NO_CREDENTIAL;
1591556Srgrimes
1601556Srgrimes	major_status = m->gm_accept_sec_context(minor_status,
1611556Srgrimes	    &ctx->gc_ctx,
1621556Srgrimes	    acceptor_mc,
1631556Srgrimes	    input_token,
164	    input_chan_bindings,
165	    &src_mn,
166	    mech_type,
167	    output_token,
168	    &mech_ret_flags,
169	    time_rec,
170	    &delegated_mc);
171	if (major_status != GSS_S_COMPLETE &&
172	    major_status != GSS_S_CONTINUE_NEEDED)
173		return (major_status);
174
175	if (!src_name) {
176		m->gm_release_name(minor_status, &src_mn);
177	} else {
178		/*
179		 * Make a new name and mark it as an MN.
180		 */
181		struct _gss_name *name = _gss_make_name(m, src_mn);
182
183		if (!name) {
184			m->gm_release_name(minor_status, &src_mn);
185			return (GSS_S_FAILURE);
186		}
187		*src_name = (gss_name_t) name;
188	}
189
190	if (delegated_mc == GSS_C_NO_CREDENTIAL)
191		mech_ret_flags &= ~GSS_C_DELEG_FLAG;
192
193	if (mech_ret_flags & GSS_C_DELEG_FLAG) {
194		if (!delegated_cred_handle) {
195			m->gm_release_cred(minor_status, &delegated_mc);
196			mech_ret_flags &= ~GSS_C_DELEG_FLAG;
197		} else {
198			struct _gss_cred *cred;
199			struct _gss_mechanism_cred *mc;
200
201			cred = malloc(sizeof(struct _gss_cred));
202			if (!cred) {
203				*minor_status = ENOMEM;
204				return (GSS_S_FAILURE);
205			}
206			SLIST_INIT(&cred->gc_mc);
207			mc = malloc(sizeof(struct _gss_mechanism_cred));
208			if (!mc) {
209				free(cred);
210				*minor_status = ENOMEM;
211				return (GSS_S_FAILURE);
212			}
213			m->gm_inquire_cred(minor_status, delegated_mc,
214			    0, 0, &cred->gc_usage, 0);
215			mc->gmc_mech = m;
216			mc->gmc_mech_oid = &m->gm_mech_oid;
217			mc->gmc_cred = delegated_mc;
218			SLIST_INSERT_HEAD(&cred->gc_mc, mc, gmc_link);
219
220			*delegated_cred_handle = (gss_cred_id_t) cred;
221		}
222	}
223
224	if (ret_flags)
225		*ret_flags = mech_ret_flags;
226	*context_handle = (gss_ctx_id_t) ctx;
227	return (major_status);
228}
229