Path: blob/main/crypto/heimdal/lib/gssapi/mech/gss_init_sec_context.c
34907 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_init_sec_context.c,v 1.1 2005/12/29 14:40:20 dfr Exp $26*/2728#include "mech_locl.h"2930static gss_cred_id_t31_gss_mech_cred_find(gss_cred_id_t cred_handle, gss_OID mech_type)32{33struct _gss_cred *cred = (struct _gss_cred *)cred_handle;34struct _gss_mechanism_cred *mc;3536if (cred == NULL)37return GSS_C_NO_CREDENTIAL;3839HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {40if (gss_oid_equal(mech_type, mc->gmc_mech_oid))41return mc->gmc_cred;42}43return GSS_C_NO_CREDENTIAL;44}4546/**47* As the initiator build a context with an acceptor.48*49* Returns in the major50* - GSS_S_COMPLETE - if the context if build51* - GSS_S_CONTINUE_NEEDED - if the caller needs to continue another52* round of gss_i nit_sec_context53* - error code - any other error code54*55* @param minor_status minor status code.56*57* @param initiator_cred_handle the credential to use when building58* the context, if GSS_C_NO_CREDENTIAL is passed, the default59* credential for the mechanism will be used.60*61* @param context_handle a pointer to a context handle, will be62* returned as long as there is not an error.63*64* @param target_name the target name of acceptor, created using65* gss_import_name(). The name is can be of any name types the66* mechanism supports, check supported name types with67* gss_inquire_names_for_mech().68*69* @param input_mech_type mechanism type to use, if GSS_C_NO_OID is70* used, Kerberos (GSS_KRB5_MECHANISM) will be tried. Other71* available mechanism are listed in the @ref gssapi_mechs_intro72* section.73*74* @param req_flags flags using when building the context, see @ref75* gssapi_context_flags76*77* @param time_req time requested this context should be valid in78* seconds, common used value is GSS_C_INDEFINITE79*80* @param input_chan_bindings Channel bindings used, if not exepected81* otherwise, used GSS_C_NO_CHANNEL_BINDINGS82*83* @param input_token input token sent from the acceptor, for the84* initial packet the buffer of { NULL, 0 } should be used.85*86* @param actual_mech_type the actual mech used, MUST NOT be freed87* since it pointing to static memory.88*89* @param output_token if there is an output token, regardless of90* complete, continue_needed, or error it should be sent to the91* acceptor92*93* @param ret_flags return what flags was negotitated, caller should94* check if they are accetable. For example, if95* GSS_C_MUTUAL_FLAG was negotiated with the acceptor or not.96*97* @param time_rec amount of time this context is valid for98*99* @returns a gss_error code, see gss_display_status() about printing100* the error code.101*102* @ingroup gssapi103*/104105106107GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL108gss_init_sec_context(OM_uint32 * minor_status,109const gss_cred_id_t initiator_cred_handle,110gss_ctx_id_t * context_handle,111const gss_name_t target_name,112const gss_OID input_mech_type,113OM_uint32 req_flags,114OM_uint32 time_req,115const gss_channel_bindings_t input_chan_bindings,116const gss_buffer_t input_token,117gss_OID * actual_mech_type,118gss_buffer_t output_token,119OM_uint32 * ret_flags,120OM_uint32 * time_rec)121{122OM_uint32 major_status;123gssapi_mech_interface m;124struct _gss_name *name = (struct _gss_name *) target_name;125struct _gss_mechanism_name *mn;126struct _gss_context *ctx = (struct _gss_context *) *context_handle;127gss_cred_id_t cred_handle;128int allocated_ctx;129gss_OID mech_type = input_mech_type;130131*minor_status = 0;132133_mg_buffer_zero(output_token);134if (actual_mech_type)135*actual_mech_type = GSS_C_NO_OID;136if (ret_flags)137*ret_flags = 0;138if (time_rec)139*time_rec = 0;140141/*142* If we haven't allocated a context yet, do so now and lookup143* the mechanism switch table. If we have one already, make144* sure we use the same mechanism switch as before.145*/146if (!ctx) {147if (mech_type == NULL)148mech_type = GSS_KRB5_MECHANISM;149150ctx = malloc(sizeof(struct _gss_context));151if (!ctx) {152*minor_status = ENOMEM;153return (GSS_S_FAILURE);154}155memset(ctx, 0, sizeof(struct _gss_context));156m = ctx->gc_mech = __gss_get_mechanism(mech_type);157if (!m) {158free(ctx);159return (GSS_S_BAD_MECH);160}161allocated_ctx = 1;162} else {163m = ctx->gc_mech;164mech_type = &ctx->gc_mech->gm_mech_oid;165allocated_ctx = 0;166}167168/*169* Find the MN for this mechanism.170*/171major_status = _gss_find_mn(minor_status, name, mech_type, &mn);172if (major_status != GSS_S_COMPLETE) {173if (allocated_ctx)174free(ctx);175return major_status;176}177178/*179* If we have a cred, find the cred for this mechanism.180*/181if (m->gm_flags & GM_USE_MG_CRED)182cred_handle = initiator_cred_handle;183else184cred_handle = _gss_mech_cred_find(initiator_cred_handle, mech_type);185186major_status = m->gm_init_sec_context(minor_status,187cred_handle,188&ctx->gc_ctx,189mn->gmn_name,190mech_type,191req_flags,192time_req,193input_chan_bindings,194input_token,195actual_mech_type,196output_token,197ret_flags,198time_rec);199200if (major_status != GSS_S_COMPLETE201&& major_status != GSS_S_CONTINUE_NEEDED) {202if (allocated_ctx)203free(ctx);204_mg_buffer_zero(output_token);205_gss_mg_error(m, major_status, *minor_status);206} else {207*context_handle = (gss_ctx_id_t) ctx;208}209210return (major_status);211}212213214