Path: blob/main/crypto/heimdal/lib/gssapi/krb5/export_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_export_sec_context (37OM_uint32 * minor_status,38gss_ctx_id_t * context_handle,39gss_buffer_t interprocess_token40)41{42krb5_context context;43const gsskrb5_ctx ctx = (const gsskrb5_ctx) *context_handle;44krb5_storage *sp;45krb5_auth_context ac;46OM_uint32 ret = GSS_S_COMPLETE;47krb5_data data;48gss_buffer_desc buffer;49int flags;50OM_uint32 minor;51krb5_error_code kret;5253GSSAPI_KRB5_INIT (&context);5455HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);5657if (!(ctx->flags & GSS_C_TRANS_FLAG)) {58HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);59*minor_status = 0;60return GSS_S_UNAVAILABLE;61}6263sp = krb5_storage_emem ();64if (sp == NULL) {65HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);66*minor_status = ENOMEM;67return GSS_S_FAILURE;68}69ac = ctx->auth_context;7071/* flagging included fields */7273flags = 0;74if (ac->local_address)75flags |= SC_LOCAL_ADDRESS;76if (ac->remote_address)77flags |= SC_REMOTE_ADDRESS;78if (ac->keyblock)79flags |= SC_KEYBLOCK;80if (ac->local_subkey)81flags |= SC_LOCAL_SUBKEY;82if (ac->remote_subkey)83flags |= SC_REMOTE_SUBKEY;8485kret = krb5_store_int32 (sp, flags);86if (kret) {87*minor_status = kret;88goto failure;89}9091/* marshall auth context */9293kret = krb5_store_int32 (sp, ac->flags);94if (kret) {95*minor_status = kret;96goto failure;97}98if (ac->local_address) {99kret = krb5_store_address (sp, *ac->local_address);100if (kret) {101*minor_status = kret;102goto failure;103}104}105if (ac->remote_address) {106kret = krb5_store_address (sp, *ac->remote_address);107if (kret) {108*minor_status = kret;109goto failure;110}111}112kret = krb5_store_int16 (sp, ac->local_port);113if (kret) {114*minor_status = kret;115goto failure;116}117kret = krb5_store_int16 (sp, ac->remote_port);118if (kret) {119*minor_status = kret;120goto failure;121}122if (ac->keyblock) {123kret = krb5_store_keyblock (sp, *ac->keyblock);124if (kret) {125*minor_status = kret;126goto failure;127}128}129if (ac->local_subkey) {130kret = krb5_store_keyblock (sp, *ac->local_subkey);131if (kret) {132*minor_status = kret;133goto failure;134}135}136if (ac->remote_subkey) {137kret = krb5_store_keyblock (sp, *ac->remote_subkey);138if (kret) {139*minor_status = kret;140goto failure;141}142}143kret = krb5_store_int32 (sp, ac->local_seqnumber);144if (kret) {145*minor_status = kret;146goto failure;147}148kret = krb5_store_int32 (sp, ac->remote_seqnumber);149if (kret) {150*minor_status = kret;151goto failure;152}153154kret = krb5_store_int32 (sp, ac->keytype);155if (kret) {156*minor_status = kret;157goto failure;158}159kret = krb5_store_int32 (sp, ac->cksumtype);160if (kret) {161*minor_status = kret;162goto failure;163}164165/* names */166167ret = _gsskrb5_export_name (minor_status,168(gss_name_t)ctx->source, &buffer);169if (ret)170goto failure;171data.data = buffer.value;172data.length = buffer.length;173kret = krb5_store_data (sp, data);174_gsskrb5_release_buffer (&minor, &buffer);175if (kret) {176*minor_status = kret;177goto failure;178}179180ret = _gsskrb5_export_name (minor_status,181(gss_name_t)ctx->target, &buffer);182if (ret)183goto failure;184data.data = buffer.value;185data.length = buffer.length;186187ret = GSS_S_FAILURE;188189kret = krb5_store_data (sp, data);190_gsskrb5_release_buffer (&minor, &buffer);191if (kret) {192*minor_status = kret;193goto failure;194}195196kret = krb5_store_int32 (sp, ctx->flags);197if (kret) {198*minor_status = kret;199goto failure;200}201kret = krb5_store_int32 (sp, ctx->more_flags);202if (kret) {203*minor_status = kret;204goto failure;205}206kret = krb5_store_int32 (sp, ctx->lifetime);207if (kret) {208*minor_status = kret;209goto failure;210}211kret = _gssapi_msg_order_export(sp, ctx->order);212if (kret ) {213*minor_status = kret;214goto failure;215}216217kret = krb5_storage_to_data (sp, &data);218krb5_storage_free (sp);219if (kret) {220HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);221*minor_status = kret;222return GSS_S_FAILURE;223}224interprocess_token->length = data.length;225interprocess_token->value = data.data;226HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);227ret = _gsskrb5_delete_sec_context (minor_status, context_handle,228GSS_C_NO_BUFFER);229if (ret != GSS_S_COMPLETE)230_gsskrb5_release_buffer (NULL, interprocess_token);231*minor_status = 0;232return ret;233failure:234HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);235krb5_storage_free (sp);236return ret;237}238239240