2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <gssapi/gssapi.h>
36 #include "mech_switch.h"
43 parse_header(const gss_buffer_t input_token, gss_OID mech_oid)
45 unsigned char *p = input_token->value;
46 size_t len = input_token->length;
50 * Token must start with [APPLICATION 0] SEQUENCE.
51 * But if it doesn't assume it is DCE-STYLE Kerberos!
54 return (GSS_S_DEFECTIVE_TOKEN);
60 * Decode the length and make sure it agrees with the
64 return (GSS_S_DEFECTIVE_TOKEN);
65 if ((*p & 0x80) == 0) {
74 return (GSS_S_DEFECTIVE_TOKEN);
84 return (GSS_S_DEFECTIVE_TOKEN);
87 * Decode the OID for the mechanism. Simplify life by
88 * assuming that the OID length is less than 128 bytes.
90 if (len < 2 || *p != 0x06)
91 return (GSS_S_DEFECTIVE_TOKEN);
92 if ((p[1] & 0x80) || p[1] > (len - 2))
93 return (GSS_S_DEFECTIVE_TOKEN);
94 mech_oid->length = p[1];
97 mech_oid->elements = p;
99 return (GSS_S_COMPLETE);
102 static gss_OID_desc krb5_mechanism =
103 {9, __DECONST(void *, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02")};
104 static gss_OID_desc ntlm_mechanism =
105 {10, __DECONST(void *, "\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a")};
106 static gss_OID_desc spnego_mechanism =
107 {6, __DECONST(void *, "\x2b\x06\x01\x05\x05\x02")};
110 choose_mech(const gss_buffer_t input, gss_OID mech_oid)
115 * First try to parse the gssapi token header and see if it's a
116 * correct header, use that in the first hand.
119 status = parse_header(input, mech_oid);
120 if (status == GSS_S_COMPLETE)
121 return (GSS_S_COMPLETE);
124 * Lets guess what mech is really is, callback function to mech ??
127 if (input->length > 8 &&
128 memcmp((const char *)input->value, "NTLMSSP\x00", 8) == 0)
130 *mech_oid = ntlm_mechanism;
131 return (GSS_S_COMPLETE);
132 } else if (input->length != 0 &&
133 ((const char *)input->value)[0] == 0x6E)
135 /* Could be a raw AP-REQ (check for APPLICATION tag) */
136 *mech_oid = krb5_mechanism;
137 return (GSS_S_COMPLETE);
138 } else if (input->length == 0) {
140 * There is the a weird mode of SPNEGO (in CIFS and
141 * SASL GSS-SPENGO where the first token is zero
142 * length and the acceptor returns a mech_list, lets
143 * hope that is what is happening now.
145 *mech_oid = spnego_mechanism;
146 return (GSS_S_COMPLETE);
151 OM_uint32 gss_accept_sec_context(OM_uint32 *minor_status,
152 gss_ctx_id_t *context_handle,
153 const gss_cred_id_t acceptor_cred_handle,
154 const gss_buffer_t input_token,
155 const gss_channel_bindings_t input_chan_bindings,
156 gss_name_t *src_name,
158 gss_buffer_t output_token,
159 OM_uint32 *ret_flags,
161 gss_cred_id_t *delegated_cred_handle)
163 OM_uint32 major_status, mech_ret_flags;
164 struct _gss_mech_switch *m;
165 struct _gss_context *ctx = (struct _gss_context *) *context_handle;
166 struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;
167 struct _gss_mechanism_cred *mc;
168 gss_cred_id_t acceptor_mc, delegated_mc;
173 *src_name = GSS_C_NO_NAME;
175 *mech_type = GSS_C_NO_OID;
180 if (delegated_cred_handle)
181 *delegated_cred_handle = GSS_C_NO_CREDENTIAL;
182 _gss_buffer_zero(output_token);
185 * If this is the first call (*context_handle is NULL), we must
186 * parse the input token to figure out the mechanism to use.
188 if (*context_handle == GSS_C_NO_CONTEXT) {
189 gss_OID_desc mech_oid;
191 major_status = choose_mech(input_token, &mech_oid);
192 if (major_status != GSS_S_COMPLETE)
193 return (major_status);
196 * Now that we have a mechanism, we can find the
199 ctx = malloc(sizeof(struct _gss_context));
201 *minor_status = ENOMEM;
202 return (GSS_S_DEFECTIVE_TOKEN);
204 memset(ctx, 0, sizeof(struct _gss_context));
205 m = ctx->gc_mech = _gss_find_mech_switch(&mech_oid);
208 return (GSS_S_BAD_MECH);
214 SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
215 if (mc->gmc_mech == m)
218 return (GSS_S_BAD_MECH);
219 acceptor_mc = mc->gmc_cred;
221 acceptor_mc = GSS_C_NO_CREDENTIAL;
223 delegated_mc = GSS_C_NO_CREDENTIAL;
226 major_status = m->gm_accept_sec_context(minor_status,
237 if (major_status != GSS_S_COMPLETE &&
238 major_status != GSS_S_CONTINUE_NEEDED) {
239 _gss_mg_error(m, major_status, *minor_status);
240 return (major_status);
243 if (src_name && src_mn) {
245 * Make a new name and mark it as an MN.
247 struct _gss_name *name = _gss_make_name(m, src_mn);
250 m->gm_release_name(minor_status, &src_mn);
251 return (GSS_S_FAILURE);
253 *src_name = (gss_name_t) name;
255 m->gm_release_name(minor_status, &src_mn);
258 if (delegated_mc == GSS_C_NO_CREDENTIAL)
259 mech_ret_flags &= ~GSS_C_DELEG_FLAG;
261 if (mech_ret_flags & GSS_C_DELEG_FLAG) {
262 if (!delegated_cred_handle) {
263 m->gm_release_cred(minor_status, &delegated_mc);
264 mech_ret_flags &= ~GSS_C_DELEG_FLAG;
266 struct _gss_cred *dcred;
267 struct _gss_mechanism_cred *dmc;
269 dcred = malloc(sizeof(struct _gss_cred));
271 *minor_status = ENOMEM;
272 return (GSS_S_FAILURE);
274 SLIST_INIT(&dcred->gc_mc);
275 dmc = malloc(sizeof(struct _gss_mechanism_cred));
278 *minor_status = ENOMEM;
279 return (GSS_S_FAILURE);
282 dmc->gmc_mech_oid = &m->gm_mech_oid;
283 dmc->gmc_cred = delegated_mc;
284 SLIST_INSERT_HEAD(&dcred->gc_mc, dmc, gmc_link);
286 *delegated_cred_handle = (gss_cred_id_t) dcred;
291 *ret_flags = mech_ret_flags;
292 *context_handle = (gss_ctx_id_t) ctx;
293 return (major_status);