r19681: Update to current lorikeet-heimdal. I'm looking at using the realm
[Samba.git] / source / heimdal / lib / gssapi / mech / gss_accept_sec_context.c
blob73207806a03a48c48b6cebdb9921c0228e31618a
1 /*-
2 * Copyright (c) 2005 Doug Rabson
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD: src/lib/libgssapi/gss_accept_sec_context.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
29 #include "mech_locl.h"
30 RCSID("$Id: gss_accept_sec_context.c,v 1.7 2006/11/10 03:30:12 lha Exp $");
32 static OM_uint32
33 parse_header(const gss_buffer_t input_token, gss_OID mech_oid)
35 unsigned char *p = input_token->value;
36 size_t len = input_token->length;
37 size_t a, b;
40 * Token must start with [APPLICATION 0] SEQUENCE.
41 * But if it doesn't assume its DCE-STYLE Kerberos!
43 if (len == 0)
44 return (GSS_S_DEFECTIVE_TOKEN);
46 p++;
47 len--;
50 * Decode the length and make sure it agrees with the
51 * token length.
53 if (len == 0)
54 return (GSS_S_DEFECTIVE_TOKEN);
55 if ((*p & 0x80) == 0) {
56 a = *p;
57 p++;
58 len--;
59 } else {
60 b = *p & 0x7f;
61 p++;
62 len--;
63 if (len < b)
64 return (GSS_S_DEFECTIVE_TOKEN);
65 a = 0;
66 while (b) {
67 a = (a << 8) | *p;
68 p++;
69 len--;
70 b--;
73 if (a != len)
74 return (GSS_S_DEFECTIVE_TOKEN);
77 * Decode the OID for the mechanism. Simplify life by
78 * assuming that the OID length is less than 128 bytes.
80 if (len < 2 || *p != 0x06)
81 return (GSS_S_DEFECTIVE_TOKEN);
82 if ((p[1] & 0x80) || p[1] > (len - 2))
83 return (GSS_S_DEFECTIVE_TOKEN);
84 mech_oid->length = p[1];
85 p += 2;
86 len -= 2;
87 mech_oid->elements = p;
89 return GSS_S_COMPLETE;
92 static gss_OID_desc krb5_mechanism =
93 {9, rk_UNCONST("\x2a\x86\x48\x86\xf7\x12\x01\x02\x02")};
94 static gss_OID_desc spnego_mechanism =
95 {6, rk_UNCONST("\x2b\x06\x01\x05\x05\x02")};
97 static OM_uint32
98 choose_mech(const gss_buffer_t input, gss_OID mech_oid)
100 OM_uint32 status;
103 * First try to parse the gssapi token header and see if its a
104 * correct header, use that in the first hand.
107 status = parse_header(input, mech_oid);
108 if (status == GSS_S_COMPLETE)
109 return GSS_S_COMPLETE;
112 * Lets guess what mech is really is, callback function to mech ??
115 if (input->length != 0 && ((const char *)input->value)[0] == 0x6E) {
116 /* Could be a raw AP-REQ (check for APPLICATION tag) */
117 *mech_oid = krb5_mechanism;
118 return GSS_S_COMPLETE;
119 } else if (input->length == 0) {
121 * There is the a wiered mode of SPNEGO (in CIFS and
122 * SASL GSS-SPENGO where the first token is zero
123 * length and the acceptor returns a mech_list, lets
124 * home that is what is happening now.
126 *mech_oid = spnego_mechanism;
127 return GSS_S_COMPLETE;
129 return status;
133 OM_uint32 gss_accept_sec_context(OM_uint32 *minor_status,
134 gss_ctx_id_t *context_handle,
135 const gss_cred_id_t acceptor_cred_handle,
136 const gss_buffer_t input_token,
137 const gss_channel_bindings_t input_chan_bindings,
138 gss_name_t *src_name,
139 gss_OID *mech_type,
140 gss_buffer_t output_token,
141 OM_uint32 *ret_flags,
142 OM_uint32 *time_rec,
143 gss_cred_id_t *delegated_cred_handle)
145 OM_uint32 major_status, mech_ret_flags;
146 gssapi_mech_interface m;
147 struct _gss_context *ctx = (struct _gss_context *) *context_handle;
148 struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;
149 struct _gss_mechanism_cred *mc;
150 gss_cred_id_t acceptor_mc, delegated_mc;
151 gss_name_t src_mn;
152 int allocated_ctx;
154 *minor_status = 0;
155 if (src_name) *src_name = 0;
156 if (mech_type) *mech_type = 0;
157 if (ret_flags) *ret_flags = 0;
158 if (time_rec) *time_rec = 0;
159 if (delegated_cred_handle) *delegated_cred_handle = 0;
160 output_token->length = 0;
161 output_token->value = 0;
164 * If this is the first call (*context_handle is NULL), we must
165 * parse the input token to figure out the mechanism to use.
167 if (*context_handle == GSS_C_NO_CONTEXT) {
168 gss_OID_desc mech_oid;
170 major_status = choose_mech(input_token, &mech_oid);
171 if (major_status != GSS_S_COMPLETE)
172 return major_status;
175 * Now that we have a mechanism, we can find the
176 * implementation.
178 ctx = malloc(sizeof(struct _gss_context));
179 if (!ctx) {
180 *minor_status = ENOMEM;
181 return (GSS_S_DEFECTIVE_TOKEN);
183 memset(ctx, 0, sizeof(struct _gss_context));
184 m = ctx->gc_mech = __gss_get_mechanism(&mech_oid);
185 if (!m) {
186 free(ctx);
187 return (GSS_S_BAD_MECH);
189 allocated_ctx = 1;
190 } else {
191 m = ctx->gc_mech;
192 allocated_ctx = 0;
195 if (cred) {
196 SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
197 if (mc->gmc_mech == m)
198 break;
199 if (!mc)
200 return (GSS_S_BAD_MECH);
201 acceptor_mc = mc->gmc_cred;
202 } else {
203 acceptor_mc = GSS_C_NO_CREDENTIAL;
205 delegated_mc = GSS_C_NO_CREDENTIAL;
207 mech_ret_flags = 0;
208 major_status = m->gm_accept_sec_context(minor_status,
209 &ctx->gc_ctx,
210 acceptor_mc,
211 input_token,
212 input_chan_bindings,
213 &src_mn,
214 mech_type,
215 output_token,
216 &mech_ret_flags,
217 time_rec,
218 &delegated_mc);
219 if (major_status != GSS_S_COMPLETE &&
220 major_status != GSS_S_CONTINUE_NEEDED)
221 return (major_status);
223 if (!src_name) {
224 m->gm_release_name(minor_status, &src_mn);
225 } else {
227 * Make a new name and mark it as an MN.
229 struct _gss_name *name = _gss_make_name(m, src_mn);
231 if (!name) {
232 m->gm_release_name(minor_status, &src_mn);
233 return (GSS_S_FAILURE);
235 *src_name = (gss_name_t) name;
238 if (mech_ret_flags & GSS_C_DELEG_FLAG) {
239 if (!delegated_cred_handle) {
240 m->gm_release_cred(minor_status, &delegated_mc);
241 *ret_flags &= ~GSS_C_DELEG_FLAG;
242 } else {
243 struct _gss_cred *dcred;
244 struct _gss_mechanism_cred *dmc;
246 dcred = malloc(sizeof(struct _gss_cred));
247 if (!dcred) {
248 *minor_status = ENOMEM;
249 return (GSS_S_FAILURE);
251 SLIST_INIT(&dcred->gc_mc);
252 dmc = malloc(sizeof(struct _gss_mechanism_cred));
253 if (!dmc) {
254 free(dcred);
255 *minor_status = ENOMEM;
256 return (GSS_S_FAILURE);
258 m->gm_inquire_cred(minor_status, delegated_mc,
259 0, 0, &dcred->gc_usage, 0);
260 dmc->gmc_mech = m;
261 dmc->gmc_mech_oid = &m->gm_mech_oid;
262 dmc->gmc_cred = delegated_mc;
263 SLIST_INSERT_HEAD(&dcred->gc_mc, dmc, gmc_link);
265 *delegated_cred_handle = (gss_cred_id_t) dcred;
269 if (ret_flags)
270 *ret_flags = mech_ret_flags;
271 *context_handle = (gss_ctx_id_t) ctx;
272 return (major_status);