Path: blob/main/sys/kgssapi/gss_init_sec_context.c
39476 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2008 Isilon Inc http://www.isilon.com/4* Authors: Doug Rabson <[email protected]>5* Developed with Red Inc: Alfred Perlstein <[email protected]>6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/param.h>30#include <sys/kernel.h>31#include <sys/kobj.h>32#include <sys/lock.h>33#include <sys/malloc.h>34#include <sys/mutex.h>35#include <sys/proc.h>3637#include <kgssapi/gssapi.h>38#include <kgssapi/gssapi_impl.h>39#include <rpc/rpc.h>4041#include "gssd.h"42#include "kgss_if.h"4344/*45* This function should only be called when the gssd46* daemon running on the system is an old one that47* does not use gss_krb5_export_lucid_sec_context().48*/49OM_uint3250gss_init_sec_context(OM_uint32 * minor_status,51const gss_cred_id_t initiator_cred_handle,52gss_ctx_id_t * context_handle,53const gss_name_t target_name,54const gss_OID input_mech_type,55OM_uint32 req_flags,56OM_uint32 time_req,57const gss_channel_bindings_t input_chan_bindings,58const gss_buffer_t input_token,59gss_OID * actual_mech_type,60gss_buffer_t output_token,61OM_uint32 * ret_flags,62OM_uint32 * time_rec)63{64struct init_sec_context_res res;65struct init_sec_context_args args;66enum clnt_stat stat;67gss_ctx_id_t ctx = *context_handle;68CLIENT *cl;6970*minor_status = 0;7172cl = kgss_gssd_client();73if (cl == NULL)74return (GSS_S_FAILURE);7576args.uid = curthread->td_ucred->cr_uid;77if (initiator_cred_handle)78args.cred = initiator_cred_handle->handle;79else80args.cred = 0;81if (ctx)82args.ctx = ctx->handle;83else84args.ctx = 0;85args.name = target_name->handle;86args.mech_type = input_mech_type;87args.req_flags = req_flags;88args.time_req = time_req;89args.input_chan_bindings = input_chan_bindings;90if (input_token)91args.input_token = *input_token;92else {93args.input_token.length = 0;94args.input_token.value = NULL;95}9697bzero(&res, sizeof(res));98stat = gssd_init_sec_context_1(&args, &res, cl);99CLNT_RELEASE(cl);100if (stat != RPC_SUCCESS) {101*minor_status = stat;102return (GSS_S_FAILURE);103}104105if (res.major_status != GSS_S_COMPLETE106&& res.major_status != GSS_S_CONTINUE_NEEDED) {107*minor_status = res.minor_status;108xdr_free((xdrproc_t) xdr_init_sec_context_res, &res);109return (res.major_status);110}111112*minor_status = res.minor_status;113114if (!ctx) {115ctx = kgss_create_context(res.actual_mech_type);116if (!ctx) {117xdr_free((xdrproc_t) xdr_init_sec_context_res, &res);118*minor_status = 0;119return (GSS_S_BAD_MECH);120}121}122*context_handle = ctx;123ctx->handle = res.ctx;124if (actual_mech_type)125*actual_mech_type = KGSS_MECH_TYPE(ctx);126kgss_copy_buffer(&res.output_token, output_token);127if (ret_flags)128*ret_flags = res.ret_flags;129if (time_rec)130*time_rec = res.time_rec;131132xdr_free((xdrproc_t) xdr_init_sec_context_res, &res);133134/*135* If the context establishment is complete, export it from136* userland and hand the result (which includes key material137* etc.) to the kernel implementation.138*/139if (res.major_status == GSS_S_COMPLETE)140res.major_status = kgss_transfer_context(ctx, NULL);141142return (res.major_status);143}144145OM_uint32146gss_supports_lucid(uint32_t *minor_status, uint32_t *vers)147{148struct supports_lucid_res res;149enum clnt_stat stat;150CLIENT *cl;151152*minor_status = 0;153154cl = kgss_gssd_client();155if (cl == NULL)156return (GSS_S_FAILURE);157158bzero(&res, sizeof(res));159stat = gssd_supports_lucid_1(NULL, &res, cl);160CLNT_RELEASE(cl);161if (stat != RPC_SUCCESS) {162*minor_status = stat;163return (GSS_S_FAILURE);164}165166if (vers)167*vers = res.vers;168169return (res.major_status);170}171172/*173* This function should be called when the gssd daemon is174* one that uses gss_krb5_export_lucid_sec_context().175* There is a lot of code common with176* gss_init_sec_context(). However, the structures used177* are not the same and future changes may be needed for178* this one. As such, I have not factored out the common179* code.180* gss_supports_lucid() may be used to check to see if the181* gssd daemon uses gss_krb5_export_lucid_sec_context().182*/183OM_uint32184gss_init_sec_context_lucid_v1(OM_uint32 * minor_status,185const gss_cred_id_t initiator_cred_handle,186gss_ctx_id_t * context_handle,187const gss_name_t target_name,188const gss_OID input_mech_type,189OM_uint32 req_flags,190OM_uint32 time_req,191const gss_channel_bindings_t input_chan_bindings,192const gss_buffer_t input_token,193gss_OID * actual_mech_type,194gss_buffer_t output_token,195OM_uint32 * ret_flags,196OM_uint32 * time_rec)197{198struct init_sec_context_lucid_v1_res res;199struct init_sec_context_lucid_v1_args args;200enum clnt_stat stat;201gss_ctx_id_t ctx = *context_handle;202CLIENT *cl;203204*minor_status = 0;205206cl = kgss_gssd_client();207if (cl == NULL)208return (GSS_S_FAILURE);209210args.uid = curthread->td_ucred->cr_uid;211if (initiator_cred_handle)212args.cred = initiator_cred_handle->handle;213else214args.cred = 0;215if (ctx)216args.ctx = ctx->handle;217else218args.ctx = 0;219args.name = target_name->handle;220args.mech_type = input_mech_type;221args.req_flags = req_flags;222args.time_req = time_req;223args.input_chan_bindings = input_chan_bindings;224if (input_token)225args.input_token = *input_token;226else {227args.input_token.length = 0;228args.input_token.value = NULL;229}230231bzero(&res, sizeof(res));232stat = gssd_init_sec_context_lucid_v1_1(&args, &res, cl);233CLNT_RELEASE(cl);234if (stat != RPC_SUCCESS) {235*minor_status = stat;236return (GSS_S_FAILURE);237}238239if (res.major_status != GSS_S_COMPLETE240&& res.major_status != GSS_S_CONTINUE_NEEDED) {241*minor_status = res.minor_status;242xdr_free((xdrproc_t) xdr_init_sec_context_lucid_v1_res, &res);243return (res.major_status);244}245246*minor_status = res.minor_status;247248if (!ctx) {249ctx = kgss_create_context(res.actual_mech_type);250if (!ctx) {251xdr_free((xdrproc_t) xdr_init_sec_context_lucid_v1_res, &res);252*minor_status = 0;253return (GSS_S_BAD_MECH);254}255}256*context_handle = ctx;257ctx->handle = res.ctx;258if (actual_mech_type)259*actual_mech_type = KGSS_MECH_TYPE(ctx);260kgss_copy_buffer(&res.output_token, output_token);261if (ret_flags)262*ret_flags = res.ret_flags;263if (time_rec)264*time_rec = res.time_rec;265266/*267* If the context establishment is complete, export it from268* userland and hand the result (which includes key material269* etc.) to the kernel implementation.270*/271if (res.major_status == GSS_S_COMPLETE) {272res.major_status = kgss_transfer_context(ctx, &res.lucid);273if (res.major_status != GSS_S_COMPLETE)274printf("gss_init_sec_context_lucid_v1: "275"transfer failed\n");276}277278xdr_free((xdrproc_t) xdr_init_sec_context_lucid_v1_res, &res);279280return (res.major_status);281}282283284