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_import_sec_context(OM_uint32 * minor_status,const gss_buffer_t interprocess_token,gss_ctx_id_t * context_handle)37c19800e8SDoug Rabson _gsskrb5_import_sec_context (
38c19800e8SDoug Rabson OM_uint32 * minor_status,
39c19800e8SDoug Rabson const gss_buffer_t interprocess_token,
40c19800e8SDoug Rabson gss_ctx_id_t * context_handle
41c19800e8SDoug Rabson )
42c19800e8SDoug Rabson {
43c19800e8SDoug Rabson OM_uint32 ret = GSS_S_FAILURE;
44c19800e8SDoug Rabson krb5_context context;
45c19800e8SDoug Rabson krb5_error_code kret;
46c19800e8SDoug Rabson krb5_storage *sp;
47c19800e8SDoug Rabson krb5_auth_context ac;
48c19800e8SDoug Rabson krb5_address local, remote;
49c19800e8SDoug Rabson krb5_address *localp, *remotep;
50c19800e8SDoug Rabson krb5_data data;
51c19800e8SDoug Rabson gss_buffer_desc buffer;
52c19800e8SDoug Rabson krb5_keyblock keyblock;
53*ae771770SStanislav Sedov int32_t flags, tmp;
54c19800e8SDoug Rabson gsskrb5_ctx ctx;
55c19800e8SDoug Rabson gss_name_t name;
56c19800e8SDoug Rabson
57c19800e8SDoug Rabson GSSAPI_KRB5_INIT (&context);
58c19800e8SDoug Rabson
59c19800e8SDoug Rabson *context_handle = GSS_C_NO_CONTEXT;
60c19800e8SDoug Rabson
61c19800e8SDoug Rabson localp = remotep = NULL;
62c19800e8SDoug Rabson
63c19800e8SDoug Rabson sp = krb5_storage_from_mem (interprocess_token->value,
64c19800e8SDoug Rabson interprocess_token->length);
65c19800e8SDoug Rabson if (sp == NULL) {
66c19800e8SDoug Rabson *minor_status = ENOMEM;
67c19800e8SDoug Rabson return GSS_S_FAILURE;
68c19800e8SDoug Rabson }
69c19800e8SDoug Rabson
70c19800e8SDoug Rabson ctx = calloc(1, sizeof(*ctx));
71c19800e8SDoug Rabson if (ctx == NULL) {
72c19800e8SDoug Rabson *minor_status = ENOMEM;
73c19800e8SDoug Rabson krb5_storage_free (sp);
74c19800e8SDoug Rabson return GSS_S_FAILURE;
75c19800e8SDoug Rabson }
76c19800e8SDoug Rabson HEIMDAL_MUTEX_init(&ctx->ctx_id_mutex);
77c19800e8SDoug Rabson
78c19800e8SDoug Rabson kret = krb5_auth_con_init (context,
79c19800e8SDoug Rabson &ctx->auth_context);
80c19800e8SDoug Rabson if (kret) {
81c19800e8SDoug Rabson *minor_status = kret;
82c19800e8SDoug Rabson ret = GSS_S_FAILURE;
83c19800e8SDoug Rabson goto failure;
84c19800e8SDoug Rabson }
85c19800e8SDoug Rabson
86c19800e8SDoug Rabson /* flags */
87c19800e8SDoug Rabson
88c19800e8SDoug Rabson *minor_status = 0;
89c19800e8SDoug Rabson
90c19800e8SDoug Rabson if (krb5_ret_int32 (sp, &flags) != 0)
91c19800e8SDoug Rabson goto failure;
92c19800e8SDoug Rabson
93c19800e8SDoug Rabson /* retrieve the auth context */
94c19800e8SDoug Rabson
95c19800e8SDoug Rabson ac = ctx->auth_context;
96*ae771770SStanislav Sedov if (krb5_ret_int32 (sp, &tmp) != 0)
97c19800e8SDoug Rabson goto failure;
98*ae771770SStanislav Sedov ac->flags = tmp;
99c19800e8SDoug Rabson if (flags & SC_LOCAL_ADDRESS) {
100c19800e8SDoug Rabson if (krb5_ret_address (sp, localp = &local) != 0)
101c19800e8SDoug Rabson goto failure;
102c19800e8SDoug Rabson }
103c19800e8SDoug Rabson
104c19800e8SDoug Rabson if (flags & SC_REMOTE_ADDRESS) {
105c19800e8SDoug Rabson if (krb5_ret_address (sp, remotep = &remote) != 0)
106c19800e8SDoug Rabson goto failure;
107c19800e8SDoug Rabson }
108c19800e8SDoug Rabson
109c19800e8SDoug Rabson krb5_auth_con_setaddrs (context, ac, localp, remotep);
110c19800e8SDoug Rabson if (localp)
111c19800e8SDoug Rabson krb5_free_address (context, localp);
112c19800e8SDoug Rabson if (remotep)
113c19800e8SDoug Rabson krb5_free_address (context, remotep);
114c19800e8SDoug Rabson localp = remotep = NULL;
115c19800e8SDoug Rabson
116c19800e8SDoug Rabson if (krb5_ret_int16 (sp, &ac->local_port) != 0)
117c19800e8SDoug Rabson goto failure;
118c19800e8SDoug Rabson
119c19800e8SDoug Rabson if (krb5_ret_int16 (sp, &ac->remote_port) != 0)
120c19800e8SDoug Rabson goto failure;
121c19800e8SDoug Rabson if (flags & SC_KEYBLOCK) {
122c19800e8SDoug Rabson if (krb5_ret_keyblock (sp, &keyblock) != 0)
123c19800e8SDoug Rabson goto failure;
124c19800e8SDoug Rabson krb5_auth_con_setkey (context, ac, &keyblock);
125c19800e8SDoug Rabson krb5_free_keyblock_contents (context, &keyblock);
126c19800e8SDoug Rabson }
127c19800e8SDoug Rabson if (flags & SC_LOCAL_SUBKEY) {
128c19800e8SDoug Rabson if (krb5_ret_keyblock (sp, &keyblock) != 0)
129c19800e8SDoug Rabson goto failure;
130c19800e8SDoug Rabson krb5_auth_con_setlocalsubkey (context, ac, &keyblock);
131c19800e8SDoug Rabson krb5_free_keyblock_contents (context, &keyblock);
132c19800e8SDoug Rabson }
133c19800e8SDoug Rabson if (flags & SC_REMOTE_SUBKEY) {
134c19800e8SDoug Rabson if (krb5_ret_keyblock (sp, &keyblock) != 0)
135c19800e8SDoug Rabson goto failure;
136c19800e8SDoug Rabson krb5_auth_con_setremotesubkey (context, ac, &keyblock);
137c19800e8SDoug Rabson krb5_free_keyblock_contents (context, &keyblock);
138c19800e8SDoug Rabson }
139c19800e8SDoug Rabson if (krb5_ret_uint32 (sp, &ac->local_seqnumber))
140c19800e8SDoug Rabson goto failure;
141c19800e8SDoug Rabson if (krb5_ret_uint32 (sp, &ac->remote_seqnumber))
142c19800e8SDoug Rabson goto failure;
143c19800e8SDoug Rabson
144c19800e8SDoug Rabson if (krb5_ret_int32 (sp, &tmp) != 0)
145c19800e8SDoug Rabson goto failure;
146c19800e8SDoug Rabson ac->keytype = tmp;
147c19800e8SDoug Rabson if (krb5_ret_int32 (sp, &tmp) != 0)
148c19800e8SDoug Rabson goto failure;
149c19800e8SDoug Rabson ac->cksumtype = tmp;
150c19800e8SDoug Rabson
151c19800e8SDoug Rabson /* names */
152c19800e8SDoug Rabson
153c19800e8SDoug Rabson if (krb5_ret_data (sp, &data))
154c19800e8SDoug Rabson goto failure;
155c19800e8SDoug Rabson buffer.value = data.data;
156c19800e8SDoug Rabson buffer.length = data.length;
157c19800e8SDoug Rabson
158c19800e8SDoug Rabson ret = _gsskrb5_import_name (minor_status, &buffer, GSS_C_NT_EXPORT_NAME,
159c19800e8SDoug Rabson &name);
160c19800e8SDoug Rabson if (ret) {
161c19800e8SDoug Rabson ret = _gsskrb5_import_name (minor_status, &buffer, GSS_C_NO_OID,
162c19800e8SDoug Rabson &name);
163c19800e8SDoug Rabson if (ret) {
164c19800e8SDoug Rabson krb5_data_free (&data);
165c19800e8SDoug Rabson goto failure;
166c19800e8SDoug Rabson }
167c19800e8SDoug Rabson }
168c19800e8SDoug Rabson ctx->source = (krb5_principal)name;
169c19800e8SDoug Rabson krb5_data_free (&data);
170c19800e8SDoug Rabson
171c19800e8SDoug Rabson if (krb5_ret_data (sp, &data) != 0)
172c19800e8SDoug Rabson goto failure;
173c19800e8SDoug Rabson buffer.value = data.data;
174c19800e8SDoug Rabson buffer.length = data.length;
175c19800e8SDoug Rabson
176c19800e8SDoug Rabson ret = _gsskrb5_import_name (minor_status, &buffer, GSS_C_NT_EXPORT_NAME,
177c19800e8SDoug Rabson &name);
178c19800e8SDoug Rabson if (ret) {
179c19800e8SDoug Rabson ret = _gsskrb5_import_name (minor_status, &buffer, GSS_C_NO_OID,
180c19800e8SDoug Rabson &name);
181c19800e8SDoug Rabson if (ret) {
182c19800e8SDoug Rabson krb5_data_free (&data);
183c19800e8SDoug Rabson goto failure;
184c19800e8SDoug Rabson }
185c19800e8SDoug Rabson }
186c19800e8SDoug Rabson ctx->target = (krb5_principal)name;
187c19800e8SDoug Rabson krb5_data_free (&data);
188c19800e8SDoug Rabson
189c19800e8SDoug Rabson if (krb5_ret_int32 (sp, &tmp))
190c19800e8SDoug Rabson goto failure;
191c19800e8SDoug Rabson ctx->flags = tmp;
192c19800e8SDoug Rabson if (krb5_ret_int32 (sp, &tmp))
193c19800e8SDoug Rabson goto failure;
194c19800e8SDoug Rabson ctx->more_flags = tmp;
195c19800e8SDoug Rabson if (krb5_ret_int32 (sp, &tmp))
196c19800e8SDoug Rabson goto failure;
197c19800e8SDoug Rabson ctx->lifetime = tmp;
198c19800e8SDoug Rabson
199c19800e8SDoug Rabson ret = _gssapi_msg_order_import(minor_status, sp, &ctx->order);
200c19800e8SDoug Rabson if (ret)
201c19800e8SDoug Rabson goto failure;
202c19800e8SDoug Rabson
203c19800e8SDoug Rabson krb5_storage_free (sp);
204c19800e8SDoug Rabson
205*ae771770SStanislav Sedov _gsskrb5i_is_cfx(context, ctx, (ctx->more_flags & LOCAL) == 0);
206*ae771770SStanislav Sedov
207c19800e8SDoug Rabson *context_handle = (gss_ctx_id_t)ctx;
208c19800e8SDoug Rabson
209c19800e8SDoug Rabson return GSS_S_COMPLETE;
210c19800e8SDoug Rabson
211c19800e8SDoug Rabson failure:
212c19800e8SDoug Rabson krb5_auth_con_free (context,
213c19800e8SDoug Rabson ctx->auth_context);
214c19800e8SDoug Rabson if (ctx->source != NULL)
215c19800e8SDoug Rabson krb5_free_principal(context, ctx->source);
216c19800e8SDoug Rabson if (ctx->target != NULL)
217c19800e8SDoug Rabson krb5_free_principal(context, ctx->target);
218c19800e8SDoug Rabson if (localp)
219c19800e8SDoug Rabson krb5_free_address (context, localp);
220c19800e8SDoug Rabson if (remotep)
221c19800e8SDoug Rabson krb5_free_address (context, remotep);
222c19800e8SDoug Rabson if(ctx->order)
223c19800e8SDoug Rabson _gssapi_msg_order_destroy(&ctx->order);
224c19800e8SDoug Rabson HEIMDAL_MUTEX_destroy(&ctx->ctx_id_mutex);
225c19800e8SDoug Rabson krb5_storage_free (sp);
226c19800e8SDoug Rabson free (ctx);
227c19800e8SDoug Rabson *context_handle = GSS_C_NO_CONTEXT;
228c19800e8SDoug Rabson return ret;
229c19800e8SDoug Rabson }
230