2 * Copyright (c) 1999 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5/gsskrb5_locl.h"
36 RCSID("$Id: export_sec_context.c 19031 2006-11-13 18:02:57Z lha $");
39 _gsskrb5_export_sec_context (
40 OM_uint32 * minor_status,
41 gss_ctx_id_t * context_handle,
42 gss_buffer_t interprocess_token
46 const gsskrb5_ctx ctx = (const gsskrb5_ctx) *context_handle;
49 OM_uint32 ret = GSS_S_COMPLETE;
51 gss_buffer_desc buffer;
56 GSSAPI_KRB5_INIT (&context);
58 HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
60 if (!(ctx->flags & GSS_C_TRANS_FLAG)) {
61 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
63 return GSS_S_UNAVAILABLE;
66 sp = krb5_storage_emem ();
68 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
69 *minor_status = ENOMEM;
72 ac = ctx->auth_context;
74 /* flagging included fields */
77 if (ac->local_address)
78 flags |= SC_LOCAL_ADDRESS;
79 if (ac->remote_address)
80 flags |= SC_REMOTE_ADDRESS;
84 flags |= SC_LOCAL_SUBKEY;
85 if (ac->remote_subkey)
86 flags |= SC_REMOTE_SUBKEY;
88 kret = krb5_store_int32 (sp, flags);
94 /* marshall auth context */
96 kret = krb5_store_int32 (sp, ac->flags);
101 if (ac->local_address) {
102 kret = krb5_store_address (sp, *ac->local_address);
104 *minor_status = kret;
108 if (ac->remote_address) {
109 kret = krb5_store_address (sp, *ac->remote_address);
111 *minor_status = kret;
115 kret = krb5_store_int16 (sp, ac->local_port);
117 *minor_status = kret;
120 kret = krb5_store_int16 (sp, ac->remote_port);
122 *minor_status = kret;
126 kret = krb5_store_keyblock (sp, *ac->keyblock);
128 *minor_status = kret;
132 if (ac->local_subkey) {
133 kret = krb5_store_keyblock (sp, *ac->local_subkey);
135 *minor_status = kret;
139 if (ac->remote_subkey) {
140 kret = krb5_store_keyblock (sp, *ac->remote_subkey);
142 *minor_status = kret;
146 kret = krb5_store_int32 (sp, ac->local_seqnumber);
148 *minor_status = kret;
151 kret = krb5_store_int32 (sp, ac->remote_seqnumber);
153 *minor_status = kret;
157 kret = krb5_store_int32 (sp, ac->keytype);
159 *minor_status = kret;
162 kret = krb5_store_int32 (sp, ac->cksumtype);
164 *minor_status = kret;
170 ret = _gsskrb5_export_name (minor_status,
171 (gss_name_t)ctx->source, &buffer);
174 data.data = buffer.value;
175 data.length = buffer.length;
176 kret = krb5_store_data (sp, data);
177 _gsskrb5_release_buffer (&minor, &buffer);
179 *minor_status = kret;
183 ret = _gsskrb5_export_name (minor_status,
184 (gss_name_t)ctx->target, &buffer);
187 data.data = buffer.value;
188 data.length = buffer.length;
192 kret = krb5_store_data (sp, data);
193 _gsskrb5_release_buffer (&minor, &buffer);
195 *minor_status = kret;
199 kret = krb5_store_int32 (sp, ctx->flags);
201 *minor_status = kret;
204 kret = krb5_store_int32 (sp, ctx->more_flags);
206 *minor_status = kret;
209 kret = krb5_store_int32 (sp, ctx->lifetime);
211 *minor_status = kret;
214 kret = _gssapi_msg_order_export(sp, ctx->order);
216 *minor_status = kret;
220 kret = krb5_storage_to_data (sp, &data);
221 krb5_storage_free (sp);
223 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
224 *minor_status = kret;
225 return GSS_S_FAILURE;
227 interprocess_token->length = data.length;
228 interprocess_token->value = data.data;
229 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
230 ret = _gsskrb5_delete_sec_context (minor_status, context_handle,
232 if (ret != GSS_S_COMPLETE)
233 _gsskrb5_release_buffer (NULL, interprocess_token);
237 HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
238 krb5_storage_free (sp);