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>
33 #include "mech_switch.h"
38 OM_uint32 gss_accept_sec_context(OM_uint32 *minor_status,
39 gss_ctx_id_t *context_handle,
40 const gss_cred_id_t acceptor_cred_handle,
41 const gss_buffer_t input_token,
42 const gss_channel_bindings_t input_chan_bindings,
45 gss_buffer_t output_token,
48 gss_cred_id_t *delegated_cred_handle)
50 OM_uint32 major_status;
51 struct _gss_mech_switch *m;
52 struct _gss_context *ctx = (struct _gss_context *) *context_handle;
53 struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;
54 struct _gss_mechanism_cred *mc;
55 gss_cred_id_t acceptor_mc, delegated_mc;
60 if (src_name) *src_name = 0;
61 if (mech_type) *mech_type = 0;
62 if (ret_flags) *ret_flags = 0;
63 if (time_rec) *time_rec = 0;
64 if (delegated_cred_handle) *delegated_cred_handle = 0;
65 output_token->length = 0;
66 output_token->value = 0;
69 * If this is the first call (*context_handle is NULL), we must
70 * parse the input token to figure out the mechanism to use.
72 if (*context_handle == GSS_C_NO_CONTEXT) {
73 unsigned char *p = input_token->value;
74 size_t len = input_token->length;
76 gss_OID_desc mech_oid;
79 * Token must start with [APPLICATION 0] SEQUENCE.
81 if (len == 0 || *p != 0x60)
82 return (GSS_S_DEFECTIVE_TOKEN);
87 * Decode the length and make sure it agrees with the
91 return (GSS_S_DEFECTIVE_TOKEN);
92 if ((*p & 0x80) == 0) {
101 return (GSS_S_DEFECTIVE_TOKEN);
111 return (GSS_S_DEFECTIVE_TOKEN);
114 * Decode the OID for the mechanism. Simplify life by
115 * assuming that the OID length is less than 128 bytes.
117 if (len < 2 || *p != 0x06)
118 return (GSS_S_DEFECTIVE_TOKEN);
119 if ((p[1] & 0x80) || p[1] > (len - 2))
120 return (GSS_S_DEFECTIVE_TOKEN);
121 mech_oid.length = p[1];
124 mech_oid.elements = p;
127 * Now that we have a mechanism, we can find the
130 ctx = malloc(sizeof(struct _gss_context));
132 *minor_status = ENOMEM;
133 return (GSS_S_DEFECTIVE_TOKEN);
135 memset(ctx, 0, sizeof(struct _gss_context));
136 m = ctx->gc_mech = _gss_find_mech_switch(&mech_oid);
139 return (GSS_S_BAD_MECH);
148 SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
149 if (mc->gmc_mech == m)
152 return (GSS_S_BAD_MECH);
153 acceptor_mc = mc->gmc_cred;
155 acceptor_mc = GSS_C_NO_CREDENTIAL;
157 delegated_mc = GSS_C_NO_CREDENTIAL;
159 major_status = m->gm_accept_sec_context(minor_status,
170 if (major_status != GSS_S_COMPLETE &&
171 major_status != GSS_S_CONTINUE_NEEDED)
172 return (major_status);
175 m->gm_release_name(minor_status, &src_mn);
178 * Make a new name and mark it as an MN.
180 struct _gss_name *name = _gss_make_name(m, src_mn);
183 m->gm_release_name(minor_status, &src_mn);
184 return (GSS_S_FAILURE);
186 *src_name = (gss_name_t) name;
189 if (*ret_flags & GSS_C_DELEG_FLAG) {
190 if (!delegated_cred_handle) {
191 m->gm_release_cred(minor_status, &delegated_mc);
192 *ret_flags &= ~GSS_C_DELEG_FLAG;
194 struct _gss_cred *cred;
195 struct _gss_mechanism_cred *mc;
197 cred = malloc(sizeof(struct _gss_cred));
199 *minor_status = ENOMEM;
200 return (GSS_S_FAILURE);
202 mc = malloc(sizeof(struct _gss_mechanism_cred));
205 *minor_status = ENOMEM;
206 return (GSS_S_FAILURE);
208 m->gm_inquire_cred(minor_status, delegated_mc,
209 0, 0, &cred->gc_usage, 0);
211 mc->gmc_mech_oid = &m->gm_mech_oid;
212 mc->gmc_cred = delegated_mc;
213 SLIST_INSERT_HEAD(&cred->gc_mc, mc, gmc_link);
215 *delegated_cred_handle = (gss_cred_id_t) cred;
219 *context_handle = (gss_ctx_id_t) ctx;
220 return (major_status);