Path: blob/main/crypto/heimdal/lib/gssapi/mech/gss_accept_sec_context.c
34914 views
/*-1* Copyright (c) 2005 Doug Rabson2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25* $FreeBSD: src/lib/libgssapi/gss_accept_sec_context.c,v 1.1 2005/12/29 14:40:20 dfr Exp $26*/2728#include "mech_locl.h"2930static OM_uint3231parse_header(const gss_buffer_t input_token, gss_OID mech_oid)32{33unsigned char *p = input_token->value;34size_t len = input_token->length;35size_t a, b;3637/*38* Token must start with [APPLICATION 0] SEQUENCE.39* But if it doesn't assume it is DCE-STYLE Kerberos!40*/41if (len == 0)42return (GSS_S_DEFECTIVE_TOKEN);4344p++;45len--;4647/*48* Decode the length and make sure it agrees with the49* token length.50*/51if (len == 0)52return (GSS_S_DEFECTIVE_TOKEN);53if ((*p & 0x80) == 0) {54a = *p;55p++;56len--;57} else {58b = *p & 0x7f;59p++;60len--;61if (len < b)62return (GSS_S_DEFECTIVE_TOKEN);63a = 0;64while (b) {65a = (a << 8) | *p;66p++;67len--;68b--;69}70}71if (a != len)72return (GSS_S_DEFECTIVE_TOKEN);7374/*75* Decode the OID for the mechanism. Simplify life by76* assuming that the OID length is less than 128 bytes.77*/78if (len < 2 || *p != 0x06)79return (GSS_S_DEFECTIVE_TOKEN);80if ((p[1] & 0x80) || p[1] > (len - 2))81return (GSS_S_DEFECTIVE_TOKEN);82mech_oid->length = p[1];83p += 2;84len -= 2;85mech_oid->elements = p;8687return GSS_S_COMPLETE;88}8990static gss_OID_desc krb5_mechanism =91{9, rk_UNCONST("\x2a\x86\x48\x86\xf7\x12\x01\x02\x02")};92static gss_OID_desc ntlm_mechanism =93{10, rk_UNCONST("\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a")};94static gss_OID_desc spnego_mechanism =95{6, rk_UNCONST("\x2b\x06\x01\x05\x05\x02")};9697static OM_uint3298choose_mech(const gss_buffer_t input, gss_OID mech_oid)99{100OM_uint32 status;101102/*103* First try to parse the gssapi token header and see if it's a104* correct header, use that in the first hand.105*/106107status = parse_header(input, mech_oid);108if (status == GSS_S_COMPLETE)109return GSS_S_COMPLETE;110111/*112* Lets guess what mech is really is, callback function to mech ??113*/114115if (input->length > 8 &&116memcmp((const char *)input->value, "NTLMSSP\x00", 8) == 0)117{118*mech_oid = ntlm_mechanism;119return GSS_S_COMPLETE;120} else if (input->length != 0 &&121((const char *)input->value)[0] == 0x6E)122{123/* Could be a raw AP-REQ (check for APPLICATION tag) */124*mech_oid = krb5_mechanism;125return GSS_S_COMPLETE;126} else if (input->length == 0) {127/*128* There is the a wierd mode of SPNEGO (in CIFS and129* SASL GSS-SPENGO where the first token is zero130* length and the acceptor returns a mech_list, lets131* hope that is what is happening now.132*133* http://msdn.microsoft.com/en-us/library/cc213114.aspx134* "NegTokenInit2 Variation for Server-Initiation"135*/136*mech_oid = spnego_mechanism;137return GSS_S_COMPLETE;138}139return status;140}141142143GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL144gss_accept_sec_context(OM_uint32 *minor_status,145gss_ctx_id_t *context_handle,146const gss_cred_id_t acceptor_cred_handle,147const gss_buffer_t input_token,148const gss_channel_bindings_t input_chan_bindings,149gss_name_t *src_name,150gss_OID *mech_type,151gss_buffer_t output_token,152OM_uint32 *ret_flags,153OM_uint32 *time_rec,154gss_cred_id_t *delegated_cred_handle)155{156OM_uint32 major_status, mech_ret_flags, junk;157gssapi_mech_interface m;158struct _gss_context *ctx = (struct _gss_context *) *context_handle;159struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;160struct _gss_mechanism_cred *mc;161gss_cred_id_t acceptor_mc, delegated_mc;162gss_name_t src_mn;163gss_OID mech_ret_type = NULL;164165*minor_status = 0;166if (src_name)167*src_name = GSS_C_NO_NAME;168if (mech_type)169*mech_type = GSS_C_NO_OID;170if (ret_flags)171*ret_flags = 0;172if (time_rec)173*time_rec = 0;174if (delegated_cred_handle)175*delegated_cred_handle = GSS_C_NO_CREDENTIAL;176_mg_buffer_zero(output_token);177178179/*180* If this is the first call (*context_handle is NULL), we must181* parse the input token to figure out the mechanism to use.182*/183if (*context_handle == GSS_C_NO_CONTEXT) {184gss_OID_desc mech_oid;185186major_status = choose_mech(input_token, &mech_oid);187if (major_status != GSS_S_COMPLETE)188return major_status;189190/*191* Now that we have a mechanism, we can find the192* implementation.193*/194ctx = malloc(sizeof(struct _gss_context));195if (!ctx) {196*minor_status = ENOMEM;197return (GSS_S_DEFECTIVE_TOKEN);198}199memset(ctx, 0, sizeof(struct _gss_context));200m = ctx->gc_mech = __gss_get_mechanism(&mech_oid);201if (!m) {202free(ctx);203return (GSS_S_BAD_MECH);204}205*context_handle = (gss_ctx_id_t) ctx;206} else {207m = ctx->gc_mech;208}209210if (cred) {211HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)212if (mc->gmc_mech == m)213break;214if (!mc) {215gss_delete_sec_context(&junk, context_handle, NULL);216return (GSS_S_BAD_MECH);217}218acceptor_mc = mc->gmc_cred;219} else {220acceptor_mc = GSS_C_NO_CREDENTIAL;221}222delegated_mc = GSS_C_NO_CREDENTIAL;223224mech_ret_flags = 0;225major_status = m->gm_accept_sec_context(minor_status,226&ctx->gc_ctx,227acceptor_mc,228input_token,229input_chan_bindings,230&src_mn,231&mech_ret_type,232output_token,233&mech_ret_flags,234time_rec,235&delegated_mc);236if (major_status != GSS_S_COMPLETE &&237major_status != GSS_S_CONTINUE_NEEDED)238{239_gss_mg_error(m, major_status, *minor_status);240gss_delete_sec_context(&junk, context_handle, NULL);241return (major_status);242}243244if (mech_type)245*mech_type = mech_ret_type;246247if (src_name && src_mn) {248/*249* Make a new name and mark it as an MN.250*/251struct _gss_name *name = _gss_make_name(m, src_mn);252253if (!name) {254m->gm_release_name(minor_status, &src_mn);255gss_delete_sec_context(&junk, context_handle, NULL);256return (GSS_S_FAILURE);257}258*src_name = (gss_name_t) name;259} else if (src_mn) {260m->gm_release_name(minor_status, &src_mn);261}262263if (mech_ret_flags & GSS_C_DELEG_FLAG) {264if (!delegated_cred_handle) {265m->gm_release_cred(minor_status, &delegated_mc);266mech_ret_flags &=267~(GSS_C_DELEG_FLAG|GSS_C_DELEG_POLICY_FLAG);268} else if (gss_oid_equal(mech_ret_type, &m->gm_mech_oid) == 0) {269/*270* If the returned mech_type is not the same271* as the mech, assume its pseudo mech type272* and the returned type is already a273* mech-glue object274*/275*delegated_cred_handle = delegated_mc;276277} else if (delegated_mc) {278struct _gss_cred *dcred;279struct _gss_mechanism_cred *dmc;280281dcred = malloc(sizeof(struct _gss_cred));282if (!dcred) {283*minor_status = ENOMEM;284gss_delete_sec_context(&junk, context_handle, NULL);285return (GSS_S_FAILURE);286}287HEIM_SLIST_INIT(&dcred->gc_mc);288dmc = malloc(sizeof(struct _gss_mechanism_cred));289if (!dmc) {290free(dcred);291*minor_status = ENOMEM;292gss_delete_sec_context(&junk, context_handle, NULL);293return (GSS_S_FAILURE);294}295dmc->gmc_mech = m;296dmc->gmc_mech_oid = &m->gm_mech_oid;297dmc->gmc_cred = delegated_mc;298HEIM_SLIST_INSERT_HEAD(&dcred->gc_mc, dmc, gmc_link);299300*delegated_cred_handle = (gss_cred_id_t) dcred;301}302}303304if (ret_flags)305*ret_flags = mech_ret_flags;306return (major_status);307}308309310