2 * Copyright (c) 2005 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
29 #include <gssapi/gssapi.h>
34 #include "mech_switch.h"
39 OM_uint32 gss_accept_sec_context(OM_uint32 *minor_status,
40 gss_ctx_id_t *context_handle,
41 const gss_cred_id_t acceptor_cred_handle,
42 const gss_buffer_t input_token,
43 const gss_channel_bindings_t input_chan_bindings,
46 gss_buffer_t output_token,
49 gss_cred_id_t *delegated_cred_handle)
51 OM_uint32 major_status, mech_ret_flags;
52 struct _gss_mech_switch *m;
53 struct _gss_context *ctx = (struct _gss_context *) *context_handle;
54 struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;
55 struct _gss_mechanism_cred *mc;
56 gss_cred_id_t acceptor_mc, delegated_mc;
61 if (src_name) *src_name = 0;
62 if (mech_type) *mech_type = 0;
63 if (ret_flags) *ret_flags = 0;
64 if (time_rec) *time_rec = 0;
65 if (delegated_cred_handle) *delegated_cred_handle = 0;
66 output_token->length = 0;
67 output_token->value = 0;
70 * If this is the first call (*context_handle is NULL), we must
71 * parse the input token to figure out the mechanism to use.
73 if (*context_handle == GSS_C_NO_CONTEXT) {
74 unsigned char *p = input_token->value;
75 size_t len = input_token->length;
77 gss_OID_desc mech_oid;
80 * Token must start with [APPLICATION 0] SEQUENCE.
82 if (len == 0 || *p != 0x60)
83 return (GSS_S_DEFECTIVE_TOKEN);
88 * Decode the length and make sure it agrees with the
92 return (GSS_S_DEFECTIVE_TOKEN);
93 if ((*p & 0x80) == 0) {
102 return (GSS_S_DEFECTIVE_TOKEN);
112 return (GSS_S_DEFECTIVE_TOKEN);
115 * Decode the OID for the mechanism. Simplify life by
116 * assuming that the OID length is less than 128 bytes.
118 if (len < 2 || *p != 0x06)
119 return (GSS_S_DEFECTIVE_TOKEN);
120 if ((p[1] & 0x80) || p[1] > (len - 2))
121 return (GSS_S_DEFECTIVE_TOKEN);
122 mech_oid.length = p[1];
125 mech_oid.elements = p;
128 * Now that we have a mechanism, we can find the
131 ctx = malloc(sizeof(struct _gss_context));
133 *minor_status = ENOMEM;
134 return (GSS_S_DEFECTIVE_TOKEN);
136 memset(ctx, 0, sizeof(struct _gss_context));
137 m = ctx->gc_mech = _gss_find_mech_switch(&mech_oid);
140 return (GSS_S_BAD_MECH);
149 SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
150 if (mc->gmc_mech == m)
153 return (GSS_S_BAD_MECH);
154 acceptor_mc = mc->gmc_cred;
156 acceptor_mc = GSS_C_NO_CREDENTIAL;
158 delegated_mc = GSS_C_NO_CREDENTIAL;
160 major_status = m->gm_accept_sec_context(minor_status,
171 if (major_status != GSS_S_COMPLETE &&
172 major_status != GSS_S_CONTINUE_NEEDED)
173 return (major_status);
176 m->gm_release_name(minor_status, &src_mn);
179 * Make a new name and mark it as an MN.
181 struct _gss_name *name = _gss_make_name(m, src_mn);
184 m->gm_release_name(minor_status, &src_mn);
185 return (GSS_S_FAILURE);
187 *src_name = (gss_name_t) name;
190 if (delegated_mc == GSS_C_NO_CREDENTIAL)
191 mech_ret_flags &= ~GSS_C_DELEG_FLAG;
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;
198 struct _gss_cred *cred;
199 struct _gss_mechanism_cred *mc;
201 cred = malloc(sizeof(struct _gss_cred));
203 *minor_status = ENOMEM;
204 return (GSS_S_FAILURE);
206 SLIST_INIT(&cred->gc_mc);
207 mc = malloc(sizeof(struct _gss_mechanism_cred));
210 *minor_status = ENOMEM;
211 return (GSS_S_FAILURE);
213 m->gm_inquire_cred(minor_status, delegated_mc,
214 0, 0, &cred->gc_usage, 0);
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);
220 *delegated_cred_handle = (gss_cred_id_t) cred;
225 *ret_flags = mech_ret_flags;
226 *context_handle = (gss_ctx_id_t) ctx;
227 return (major_status);