1c19800e8SDoug Rabson /*
2*ae771770SStanislav Sedov * Copyright (c) 1999 - 2003 Kungliga Tekniska Högskolan
3c19800e8SDoug Rabson * (Royal Institute of Technology, Stockholm, Sweden).
4c19800e8SDoug Rabson * All rights reserved.
5c19800e8SDoug Rabson *
6c19800e8SDoug Rabson * Redistribution and use in source and binary forms, with or without
7c19800e8SDoug Rabson * modification, are permitted provided that the following conditions
8c19800e8SDoug Rabson * are met:
9c19800e8SDoug Rabson *
10c19800e8SDoug Rabson * 1. Redistributions of source code must retain the above copyright
11c19800e8SDoug Rabson * notice, this list of conditions and the following disclaimer.
12c19800e8SDoug Rabson *
13c19800e8SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
14c19800e8SDoug Rabson * notice, this list of conditions and the following disclaimer in the
15c19800e8SDoug Rabson * documentation and/or other materials provided with the distribution.
16c19800e8SDoug Rabson *
17c19800e8SDoug Rabson * 3. Neither the name of the Institute nor the names of its contributors
18c19800e8SDoug Rabson * may be used to endorse or promote products derived from this software
19c19800e8SDoug Rabson * without specific prior written permission.
20c19800e8SDoug Rabson *
21c19800e8SDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22c19800e8SDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23c19800e8SDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24c19800e8SDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25c19800e8SDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26c19800e8SDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27c19800e8SDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28c19800e8SDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29c19800e8SDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30c19800e8SDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31c19800e8SDoug Rabson * SUCH DAMAGE.
32c19800e8SDoug Rabson */
33c19800e8SDoug Rabson
34*ae771770SStanislav Sedov #include "gsskrb5_locl.h"
35c19800e8SDoug Rabson
36*ae771770SStanislav Sedov OM_uint32 GSSAPI_CALLCONV
_gsskrb5_export_sec_context(OM_uint32 * minor_status,gss_ctx_id_t * context_handle,gss_buffer_t interprocess_token)37c19800e8SDoug Rabson _gsskrb5_export_sec_context (
38c19800e8SDoug Rabson OM_uint32 * minor_status,
39c19800e8SDoug Rabson gss_ctx_id_t * context_handle,
40c19800e8SDoug Rabson gss_buffer_t interprocess_token
41c19800e8SDoug Rabson )
42c19800e8SDoug Rabson {
43c19800e8SDoug Rabson krb5_context context;
44c19800e8SDoug Rabson const gsskrb5_ctx ctx = (const gsskrb5_ctx) *context_handle;
45c19800e8SDoug Rabson krb5_storage *sp;
46c19800e8SDoug Rabson krb5_auth_context ac;
47c19800e8SDoug Rabson OM_uint32 ret = GSS_S_COMPLETE;
48c19800e8SDoug Rabson krb5_data data;
49c19800e8SDoug Rabson gss_buffer_desc buffer;
50c19800e8SDoug Rabson int flags;
51c19800e8SDoug Rabson OM_uint32 minor;
52c19800e8SDoug Rabson krb5_error_code kret;
53c19800e8SDoug Rabson
54c19800e8SDoug Rabson GSSAPI_KRB5_INIT (&context);
55c19800e8SDoug Rabson
56c19800e8SDoug Rabson HEIMDAL_MUTEX_lock(&ctx->ctx_id_mutex);
57c19800e8SDoug Rabson
58c19800e8SDoug Rabson if (!(ctx->flags & GSS_C_TRANS_FLAG)) {
59c19800e8SDoug Rabson HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
60c19800e8SDoug Rabson *minor_status = 0;
61c19800e8SDoug Rabson return GSS_S_UNAVAILABLE;
62c19800e8SDoug Rabson }
63c19800e8SDoug Rabson
64c19800e8SDoug Rabson sp = krb5_storage_emem ();
65c19800e8SDoug Rabson if (sp == NULL) {
66c19800e8SDoug Rabson HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
67c19800e8SDoug Rabson *minor_status = ENOMEM;
68c19800e8SDoug Rabson return GSS_S_FAILURE;
69c19800e8SDoug Rabson }
70c19800e8SDoug Rabson ac = ctx->auth_context;
71c19800e8SDoug Rabson
72c19800e8SDoug Rabson /* flagging included fields */
73c19800e8SDoug Rabson
74c19800e8SDoug Rabson flags = 0;
75c19800e8SDoug Rabson if (ac->local_address)
76c19800e8SDoug Rabson flags |= SC_LOCAL_ADDRESS;
77c19800e8SDoug Rabson if (ac->remote_address)
78c19800e8SDoug Rabson flags |= SC_REMOTE_ADDRESS;
79c19800e8SDoug Rabson if (ac->keyblock)
80c19800e8SDoug Rabson flags |= SC_KEYBLOCK;
81c19800e8SDoug Rabson if (ac->local_subkey)
82c19800e8SDoug Rabson flags |= SC_LOCAL_SUBKEY;
83c19800e8SDoug Rabson if (ac->remote_subkey)
84c19800e8SDoug Rabson flags |= SC_REMOTE_SUBKEY;
85c19800e8SDoug Rabson
86c19800e8SDoug Rabson kret = krb5_store_int32 (sp, flags);
87c19800e8SDoug Rabson if (kret) {
88c19800e8SDoug Rabson *minor_status = kret;
89c19800e8SDoug Rabson goto failure;
90c19800e8SDoug Rabson }
91c19800e8SDoug Rabson
92c19800e8SDoug Rabson /* marshall auth context */
93c19800e8SDoug Rabson
94c19800e8SDoug Rabson kret = krb5_store_int32 (sp, ac->flags);
95c19800e8SDoug Rabson if (kret) {
96c19800e8SDoug Rabson *minor_status = kret;
97c19800e8SDoug Rabson goto failure;
98c19800e8SDoug Rabson }
99c19800e8SDoug Rabson if (ac->local_address) {
100c19800e8SDoug Rabson kret = krb5_store_address (sp, *ac->local_address);
101c19800e8SDoug Rabson if (kret) {
102c19800e8SDoug Rabson *minor_status = kret;
103c19800e8SDoug Rabson goto failure;
104c19800e8SDoug Rabson }
105c19800e8SDoug Rabson }
106c19800e8SDoug Rabson if (ac->remote_address) {
107c19800e8SDoug Rabson kret = krb5_store_address (sp, *ac->remote_address);
108c19800e8SDoug Rabson if (kret) {
109c19800e8SDoug Rabson *minor_status = kret;
110c19800e8SDoug Rabson goto failure;
111c19800e8SDoug Rabson }
112c19800e8SDoug Rabson }
113c19800e8SDoug Rabson kret = krb5_store_int16 (sp, ac->local_port);
114c19800e8SDoug Rabson if (kret) {
115c19800e8SDoug Rabson *minor_status = kret;
116c19800e8SDoug Rabson goto failure;
117c19800e8SDoug Rabson }
118c19800e8SDoug Rabson kret = krb5_store_int16 (sp, ac->remote_port);
119c19800e8SDoug Rabson if (kret) {
120c19800e8SDoug Rabson *minor_status = kret;
121c19800e8SDoug Rabson goto failure;
122c19800e8SDoug Rabson }
123c19800e8SDoug Rabson if (ac->keyblock) {
124c19800e8SDoug Rabson kret = krb5_store_keyblock (sp, *ac->keyblock);
125c19800e8SDoug Rabson if (kret) {
126c19800e8SDoug Rabson *minor_status = kret;
127c19800e8SDoug Rabson goto failure;
128c19800e8SDoug Rabson }
129c19800e8SDoug Rabson }
130c19800e8SDoug Rabson if (ac->local_subkey) {
131c19800e8SDoug Rabson kret = krb5_store_keyblock (sp, *ac->local_subkey);
132c19800e8SDoug Rabson if (kret) {
133c19800e8SDoug Rabson *minor_status = kret;
134c19800e8SDoug Rabson goto failure;
135c19800e8SDoug Rabson }
136c19800e8SDoug Rabson }
137c19800e8SDoug Rabson if (ac->remote_subkey) {
138c19800e8SDoug Rabson kret = krb5_store_keyblock (sp, *ac->remote_subkey);
139c19800e8SDoug Rabson if (kret) {
140c19800e8SDoug Rabson *minor_status = kret;
141c19800e8SDoug Rabson goto failure;
142c19800e8SDoug Rabson }
143c19800e8SDoug Rabson }
144c19800e8SDoug Rabson kret = krb5_store_int32 (sp, ac->local_seqnumber);
145c19800e8SDoug Rabson if (kret) {
146c19800e8SDoug Rabson *minor_status = kret;
147c19800e8SDoug Rabson goto failure;
148c19800e8SDoug Rabson }
149c19800e8SDoug Rabson kret = krb5_store_int32 (sp, ac->remote_seqnumber);
150c19800e8SDoug Rabson if (kret) {
151c19800e8SDoug Rabson *minor_status = kret;
152c19800e8SDoug Rabson goto failure;
153c19800e8SDoug Rabson }
154c19800e8SDoug Rabson
155c19800e8SDoug Rabson kret = krb5_store_int32 (sp, ac->keytype);
156c19800e8SDoug Rabson if (kret) {
157c19800e8SDoug Rabson *minor_status = kret;
158c19800e8SDoug Rabson goto failure;
159c19800e8SDoug Rabson }
160c19800e8SDoug Rabson kret = krb5_store_int32 (sp, ac->cksumtype);
161c19800e8SDoug Rabson if (kret) {
162c19800e8SDoug Rabson *minor_status = kret;
163c19800e8SDoug Rabson goto failure;
164c19800e8SDoug Rabson }
165c19800e8SDoug Rabson
166c19800e8SDoug Rabson /* names */
167c19800e8SDoug Rabson
168c19800e8SDoug Rabson ret = _gsskrb5_export_name (minor_status,
169c19800e8SDoug Rabson (gss_name_t)ctx->source, &buffer);
170c19800e8SDoug Rabson if (ret)
171c19800e8SDoug Rabson goto failure;
172c19800e8SDoug Rabson data.data = buffer.value;
173c19800e8SDoug Rabson data.length = buffer.length;
174c19800e8SDoug Rabson kret = krb5_store_data (sp, data);
175c19800e8SDoug Rabson _gsskrb5_release_buffer (&minor, &buffer);
176c19800e8SDoug Rabson if (kret) {
177c19800e8SDoug Rabson *minor_status = kret;
178c19800e8SDoug Rabson goto failure;
179c19800e8SDoug Rabson }
180c19800e8SDoug Rabson
181c19800e8SDoug Rabson ret = _gsskrb5_export_name (minor_status,
182c19800e8SDoug Rabson (gss_name_t)ctx->target, &buffer);
183c19800e8SDoug Rabson if (ret)
184c19800e8SDoug Rabson goto failure;
185c19800e8SDoug Rabson data.data = buffer.value;
186c19800e8SDoug Rabson data.length = buffer.length;
187c19800e8SDoug Rabson
188c19800e8SDoug Rabson ret = GSS_S_FAILURE;
189c19800e8SDoug Rabson
190c19800e8SDoug Rabson kret = krb5_store_data (sp, data);
191c19800e8SDoug Rabson _gsskrb5_release_buffer (&minor, &buffer);
192c19800e8SDoug Rabson if (kret) {
193c19800e8SDoug Rabson *minor_status = kret;
194c19800e8SDoug Rabson goto failure;
195c19800e8SDoug Rabson }
196c19800e8SDoug Rabson
197c19800e8SDoug Rabson kret = krb5_store_int32 (sp, ctx->flags);
198c19800e8SDoug Rabson if (kret) {
199c19800e8SDoug Rabson *minor_status = kret;
200c19800e8SDoug Rabson goto failure;
201c19800e8SDoug Rabson }
202c19800e8SDoug Rabson kret = krb5_store_int32 (sp, ctx->more_flags);
203c19800e8SDoug Rabson if (kret) {
204c19800e8SDoug Rabson *minor_status = kret;
205c19800e8SDoug Rabson goto failure;
206c19800e8SDoug Rabson }
207c19800e8SDoug Rabson kret = krb5_store_int32 (sp, ctx->lifetime);
208c19800e8SDoug Rabson if (kret) {
209c19800e8SDoug Rabson *minor_status = kret;
210c19800e8SDoug Rabson goto failure;
211c19800e8SDoug Rabson }
212c19800e8SDoug Rabson kret = _gssapi_msg_order_export(sp, ctx->order);
213c19800e8SDoug Rabson if (kret ) {
214c19800e8SDoug Rabson *minor_status = kret;
215c19800e8SDoug Rabson goto failure;
216c19800e8SDoug Rabson }
217c19800e8SDoug Rabson
218c19800e8SDoug Rabson kret = krb5_storage_to_data (sp, &data);
219c19800e8SDoug Rabson krb5_storage_free (sp);
220c19800e8SDoug Rabson if (kret) {
221c19800e8SDoug Rabson HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
222c19800e8SDoug Rabson *minor_status = kret;
223c19800e8SDoug Rabson return GSS_S_FAILURE;
224c19800e8SDoug Rabson }
225c19800e8SDoug Rabson interprocess_token->length = data.length;
226c19800e8SDoug Rabson interprocess_token->value = data.data;
227c19800e8SDoug Rabson HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
228c19800e8SDoug Rabson ret = _gsskrb5_delete_sec_context (minor_status, context_handle,
229c19800e8SDoug Rabson GSS_C_NO_BUFFER);
230c19800e8SDoug Rabson if (ret != GSS_S_COMPLETE)
231c19800e8SDoug Rabson _gsskrb5_release_buffer (NULL, interprocess_token);
232c19800e8SDoug Rabson *minor_status = 0;
233c19800e8SDoug Rabson return ret;
234c19800e8SDoug Rabson failure:
235c19800e8SDoug Rabson HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);
236c19800e8SDoug Rabson krb5_storage_free (sp);
237c19800e8SDoug Rabson return ret;
238c19800e8SDoug Rabson }
239