Path: blob/main/crypto/heimdal/lib/gssapi/krb5/import_sec_context.c
34923 views
/*1* Copyright (c) 1999 - 2003 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*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* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "gsskrb5_locl.h"3435OM_uint32 GSSAPI_CALLCONV36_gsskrb5_import_sec_context (37OM_uint32 * minor_status,38const gss_buffer_t interprocess_token,39gss_ctx_id_t * context_handle40)41{42OM_uint32 ret = GSS_S_FAILURE;43krb5_context context;44krb5_error_code kret;45krb5_storage *sp;46krb5_auth_context ac;47krb5_address local, remote;48krb5_address *localp, *remotep;49krb5_data data;50gss_buffer_desc buffer;51krb5_keyblock keyblock;52int32_t flags, tmp;53gsskrb5_ctx ctx;54gss_name_t name;5556GSSAPI_KRB5_INIT (&context);5758*context_handle = GSS_C_NO_CONTEXT;5960localp = remotep = NULL;6162sp = krb5_storage_from_mem (interprocess_token->value,63interprocess_token->length);64if (sp == NULL) {65*minor_status = ENOMEM;66return GSS_S_FAILURE;67}6869ctx = calloc(1, sizeof(*ctx));70if (ctx == NULL) {71*minor_status = ENOMEM;72krb5_storage_free (sp);73return GSS_S_FAILURE;74}75HEIMDAL_MUTEX_init(&ctx->ctx_id_mutex);7677kret = krb5_auth_con_init (context,78&ctx->auth_context);79if (kret) {80*minor_status = kret;81ret = GSS_S_FAILURE;82goto failure;83}8485/* flags */8687*minor_status = 0;8889if (krb5_ret_int32 (sp, &flags) != 0)90goto failure;9192/* retrieve the auth context */9394ac = ctx->auth_context;95if (krb5_ret_int32 (sp, &tmp) != 0)96goto failure;97ac->flags = tmp;98if (flags & SC_LOCAL_ADDRESS) {99if (krb5_ret_address (sp, localp = &local) != 0)100goto failure;101}102103if (flags & SC_REMOTE_ADDRESS) {104if (krb5_ret_address (sp, remotep = &remote) != 0)105goto failure;106}107108krb5_auth_con_setaddrs (context, ac, localp, remotep);109if (localp)110krb5_free_address (context, localp);111if (remotep)112krb5_free_address (context, remotep);113localp = remotep = NULL;114115if (krb5_ret_int16 (sp, &ac->local_port) != 0)116goto failure;117118if (krb5_ret_int16 (sp, &ac->remote_port) != 0)119goto failure;120if (flags & SC_KEYBLOCK) {121if (krb5_ret_keyblock (sp, &keyblock) != 0)122goto failure;123krb5_auth_con_setkey (context, ac, &keyblock);124krb5_free_keyblock_contents (context, &keyblock);125}126if (flags & SC_LOCAL_SUBKEY) {127if (krb5_ret_keyblock (sp, &keyblock) != 0)128goto failure;129krb5_auth_con_setlocalsubkey (context, ac, &keyblock);130krb5_free_keyblock_contents (context, &keyblock);131}132if (flags & SC_REMOTE_SUBKEY) {133if (krb5_ret_keyblock (sp, &keyblock) != 0)134goto failure;135krb5_auth_con_setremotesubkey (context, ac, &keyblock);136krb5_free_keyblock_contents (context, &keyblock);137}138if (krb5_ret_uint32 (sp, &ac->local_seqnumber))139goto failure;140if (krb5_ret_uint32 (sp, &ac->remote_seqnumber))141goto failure;142143if (krb5_ret_int32 (sp, &tmp) != 0)144goto failure;145ac->keytype = tmp;146if (krb5_ret_int32 (sp, &tmp) != 0)147goto failure;148ac->cksumtype = tmp;149150/* names */151152if (krb5_ret_data (sp, &data))153goto failure;154buffer.value = data.data;155buffer.length = data.length;156157ret = _gsskrb5_import_name (minor_status, &buffer, GSS_C_NT_EXPORT_NAME,158&name);159if (ret) {160ret = _gsskrb5_import_name (minor_status, &buffer, GSS_C_NO_OID,161&name);162if (ret) {163krb5_data_free (&data);164goto failure;165}166}167ctx->source = (krb5_principal)name;168krb5_data_free (&data);169170if (krb5_ret_data (sp, &data) != 0)171goto failure;172buffer.value = data.data;173buffer.length = data.length;174175ret = _gsskrb5_import_name (minor_status, &buffer, GSS_C_NT_EXPORT_NAME,176&name);177if (ret) {178ret = _gsskrb5_import_name (minor_status, &buffer, GSS_C_NO_OID,179&name);180if (ret) {181krb5_data_free (&data);182goto failure;183}184}185ctx->target = (krb5_principal)name;186krb5_data_free (&data);187188if (krb5_ret_int32 (sp, &tmp))189goto failure;190ctx->flags = tmp;191if (krb5_ret_int32 (sp, &tmp))192goto failure;193ctx->more_flags = tmp;194if (krb5_ret_int32 (sp, &tmp))195goto failure;196ctx->lifetime = tmp;197198ret = _gssapi_msg_order_import(minor_status, sp, &ctx->order);199if (ret)200goto failure;201202krb5_storage_free (sp);203204_gsskrb5i_is_cfx(context, ctx, (ctx->more_flags & LOCAL) == 0);205206*context_handle = (gss_ctx_id_t)ctx;207208return GSS_S_COMPLETE;209210failure:211krb5_auth_con_free (context,212ctx->auth_context);213if (ctx->source != NULL)214krb5_free_principal(context, ctx->source);215if (ctx->target != NULL)216krb5_free_principal(context, ctx->target);217if (localp)218krb5_free_address (context, localp);219if (remotep)220krb5_free_address (context, remotep);221if(ctx->order)222_gssapi_msg_order_destroy(&ctx->order);223HEIMDAL_MUTEX_destroy(&ctx->ctx_id_mutex);224krb5_storage_free (sp);225free (ctx);226*context_handle = GSS_C_NO_CONTEXT;227return ret;228}229230231