gss_accept_sec_context.c revision 1.1.1.1
1/*	$NetBSD: gss_accept_sec_context.c,v 1.1.1.1 2011/04/13 18:14:46 elric Exp $	*/
2
3/*-
4 * Copyright (c) 2005 Doug Rabson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    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.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	$FreeBSD: src/lib/libgssapi/gss_accept_sec_context.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
29 */
30
31#include "mech_locl.h"
32
33static OM_uint32
34parse_header(const gss_buffer_t input_token, gss_OID mech_oid)
35{
36	unsigned char *p = input_token->value;
37	size_t len = input_token->length;
38	size_t a, b;
39
40	/*
41	 * Token must start with [APPLICATION 0] SEQUENCE.
42	 * But if it doesn't assume it is DCE-STYLE Kerberos!
43	 */
44	if (len == 0)
45		return (GSS_S_DEFECTIVE_TOKEN);
46
47	p++;
48	len--;
49
50	/*
51	 * Decode the length and make sure it agrees with the
52	 * token length.
53	 */
54	if (len == 0)
55		return (GSS_S_DEFECTIVE_TOKEN);
56	if ((*p & 0x80) == 0) {
57		a = *p;
58		p++;
59		len--;
60	} else {
61		b = *p & 0x7f;
62		p++;
63		len--;
64		if (len < b)
65		    return (GSS_S_DEFECTIVE_TOKEN);
66		a = 0;
67		while (b) {
68		    a = (a << 8) | *p;
69		    p++;
70		    len--;
71		    b--;
72		}
73	}
74	if (a != len)
75		return (GSS_S_DEFECTIVE_TOKEN);
76
77	/*
78	 * Decode the OID for the mechanism. Simplify life by
79	 * assuming that the OID length is less than 128 bytes.
80	 */
81	if (len < 2 || *p != 0x06)
82		return (GSS_S_DEFECTIVE_TOKEN);
83	if ((p[1] & 0x80) || p[1] > (len - 2))
84		return (GSS_S_DEFECTIVE_TOKEN);
85	mech_oid->length = p[1];
86	p += 2;
87	len -= 2;
88	mech_oid->elements = p;
89
90	return GSS_S_COMPLETE;
91}
92
93static gss_OID_desc krb5_mechanism =
94    {9, rk_UNCONST("\x2a\x86\x48\x86\xf7\x12\x01\x02\x02")};
95static gss_OID_desc ntlm_mechanism =
96    {10, rk_UNCONST("\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a")};
97static gss_OID_desc spnego_mechanism =
98    {6, rk_UNCONST("\x2b\x06\x01\x05\x05\x02")};
99
100static OM_uint32
101choose_mech(const gss_buffer_t input, gss_OID mech_oid)
102{
103	OM_uint32 status;
104
105	/*
106	 * First try to parse the gssapi token header and see if it's a
107	 * correct header, use that in the first hand.
108	 */
109
110	status = parse_header(input, mech_oid);
111	if (status == GSS_S_COMPLETE)
112	    return GSS_S_COMPLETE;
113
114	/*
115	 * Lets guess what mech is really is, callback function to mech ??
116	 */
117
118	if (input->length > 8 &&
119	    memcmp((const char *)input->value, "NTLMSSP\x00", 8) == 0)
120	{
121		*mech_oid = ntlm_mechanism;
122		return GSS_S_COMPLETE;
123	} else if (input->length != 0 &&
124		   ((const char *)input->value)[0] == 0x6E)
125	{
126		/* Could be a raw AP-REQ (check for APPLICATION tag) */
127		*mech_oid = krb5_mechanism;
128		return GSS_S_COMPLETE;
129	} else if (input->length == 0) {
130		/*
131		 * There is the a wierd mode of SPNEGO (in CIFS and
132		 * SASL GSS-SPENGO where the first token is zero
133		 * length and the acceptor returns a mech_list, lets
134		 * hope that is what is happening now.
135		 *
136		 * http://msdn.microsoft.com/en-us/library/cc213114.aspx
137		 * "NegTokenInit2 Variation for Server-Initiation"
138		 */
139		*mech_oid = spnego_mechanism;
140		return GSS_S_COMPLETE;
141	}
142	return status;
143}
144
145
146GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
147gss_accept_sec_context(OM_uint32 *minor_status,
148    gss_ctx_id_t *context_handle,
149    const gss_cred_id_t acceptor_cred_handle,
150    const gss_buffer_t input_token,
151    const gss_channel_bindings_t input_chan_bindings,
152    gss_name_t *src_name,
153    gss_OID *mech_type,
154    gss_buffer_t output_token,
155    OM_uint32 *ret_flags,
156    OM_uint32 *time_rec,
157    gss_cred_id_t *delegated_cred_handle)
158{
159	OM_uint32 major_status, mech_ret_flags, junk;
160	gssapi_mech_interface m;
161	struct _gss_context *ctx = (struct _gss_context *) *context_handle;
162	struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;
163	struct _gss_mechanism_cred *mc;
164	gss_cred_id_t acceptor_mc, delegated_mc;
165	gss_name_t src_mn;
166	gss_OID mech_ret_type = NULL;
167
168	*minor_status = 0;
169	if (src_name)
170	    *src_name = GSS_C_NO_NAME;
171	if (mech_type)
172	    *mech_type = GSS_C_NO_OID;
173	if (ret_flags)
174	    *ret_flags = 0;
175	if (time_rec)
176	    *time_rec = 0;
177	if (delegated_cred_handle)
178	    *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
179	_mg_buffer_zero(output_token);
180
181
182	/*
183	 * If this is the first call (*context_handle is NULL), we must
184	 * parse the input token to figure out the mechanism to use.
185	 */
186	if (*context_handle == GSS_C_NO_CONTEXT) {
187		gss_OID_desc mech_oid;
188
189		major_status = choose_mech(input_token, &mech_oid);
190		if (major_status != GSS_S_COMPLETE)
191			return major_status;
192
193		/*
194		 * Now that we have a mechanism, we can find the
195		 * implementation.
196		 */
197		ctx = malloc(sizeof(struct _gss_context));
198		if (!ctx) {
199			*minor_status = ENOMEM;
200			return (GSS_S_DEFECTIVE_TOKEN);
201		}
202		memset(ctx, 0, sizeof(struct _gss_context));
203		m = ctx->gc_mech = __gss_get_mechanism(&mech_oid);
204		if (!m) {
205			free(ctx);
206			return (GSS_S_BAD_MECH);
207		}
208		*context_handle = (gss_ctx_id_t) ctx;
209	} else {
210		m = ctx->gc_mech;
211	}
212
213	if (cred) {
214		HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
215			if (mc->gmc_mech == m)
216				break;
217		if (!mc) {
218		        gss_delete_sec_context(&junk, context_handle, NULL);
219			return (GSS_S_BAD_MECH);
220		}
221		acceptor_mc = mc->gmc_cred;
222	} else {
223		acceptor_mc = GSS_C_NO_CREDENTIAL;
224	}
225	delegated_mc = GSS_C_NO_CREDENTIAL;
226
227	mech_ret_flags = 0;
228	major_status = m->gm_accept_sec_context(minor_status,
229	    &ctx->gc_ctx,
230	    acceptor_mc,
231	    input_token,
232	    input_chan_bindings,
233	    &src_mn,
234	    &mech_ret_type,
235	    output_token,
236	    &mech_ret_flags,
237	    time_rec,
238	    &delegated_mc);
239	if (major_status != GSS_S_COMPLETE &&
240	    major_status != GSS_S_CONTINUE_NEEDED)
241	{
242		_gss_mg_error(m, major_status, *minor_status);
243		gss_delete_sec_context(&junk, context_handle, NULL);
244		return (major_status);
245	}
246
247	if (mech_type)
248	    *mech_type = mech_ret_type;
249
250	if (src_name && src_mn) {
251		/*
252		 * Make a new name and mark it as an MN.
253		 */
254		struct _gss_name *name = _gss_make_name(m, src_mn);
255
256		if (!name) {
257			m->gm_release_name(minor_status, &src_mn);
258		        gss_delete_sec_context(&junk, context_handle, NULL);
259			return (GSS_S_FAILURE);
260		}
261		*src_name = (gss_name_t) name;
262	} else if (src_mn) {
263		m->gm_release_name(minor_status, &src_mn);
264	}
265
266	if (mech_ret_flags & GSS_C_DELEG_FLAG) {
267		if (!delegated_cred_handle) {
268			m->gm_release_cred(minor_status, &delegated_mc);
269			mech_ret_flags &=
270			    ~(GSS_C_DELEG_FLAG|GSS_C_DELEG_POLICY_FLAG);
271		} else if (gss_oid_equal(mech_ret_type, &m->gm_mech_oid) == 0) {
272			/*
273			 * If the returned mech_type is not the same
274			 * as the mech, assume its pseudo mech type
275			 * and the returned type is already a
276			 * mech-glue object
277			 */
278			*delegated_cred_handle = delegated_mc;
279
280		} else if (delegated_mc) {
281			struct _gss_cred *dcred;
282			struct _gss_mechanism_cred *dmc;
283
284			dcred = malloc(sizeof(struct _gss_cred));
285			if (!dcred) {
286				*minor_status = ENOMEM;
287				gss_delete_sec_context(&junk, context_handle, NULL);
288				return (GSS_S_FAILURE);
289			}
290			HEIM_SLIST_INIT(&dcred->gc_mc);
291			dmc = malloc(sizeof(struct _gss_mechanism_cred));
292			if (!dmc) {
293				free(dcred);
294				*minor_status = ENOMEM;
295				gss_delete_sec_context(&junk, context_handle, NULL);
296				return (GSS_S_FAILURE);
297			}
298			dmc->gmc_mech = m;
299			dmc->gmc_mech_oid = &m->gm_mech_oid;
300			dmc->gmc_cred = delegated_mc;
301			HEIM_SLIST_INSERT_HEAD(&dcred->gc_mc, dmc, gmc_link);
302
303			*delegated_cred_handle = (gss_cred_id_t) dcred;
304		}
305	}
306
307	if (ret_flags)
308	    *ret_flags = mech_ret_flags;
309	return (major_status);
310}
311