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: import_sec_context.c,v 1.17 2006/10/07 22:14:53 lha Exp $");
39 _gsskrb5_import_sec_context (
40 OM_uint32
* minor_status
,
41 const gss_buffer_t interprocess_token
,
42 gss_ctx_id_t
* context_handle
45 OM_uint32 ret
= GSS_S_FAILURE
;
49 krb5_address local
, remote
;
50 krb5_address
*localp
, *remotep
;
52 gss_buffer_desc buffer
;
53 krb5_keyblock keyblock
;
61 *context_handle
= GSS_C_NO_CONTEXT
;
63 localp
= remotep
= NULL
;
65 sp
= krb5_storage_from_mem (interprocess_token
->value
,
66 interprocess_token
->length
);
68 *minor_status
= ENOMEM
;
72 ctx
= calloc(1, sizeof(*ctx
));
74 *minor_status
= ENOMEM
;
75 krb5_storage_free (sp
);
78 HEIMDAL_MUTEX_init(&ctx
->ctx_id_mutex
);
80 kret
= krb5_auth_con_init (_gsskrb5_context
,
83 _gsskrb5_set_error_string ();
93 if (krb5_ret_int32 (sp
, &flags
) != 0)
96 /* retrieve the auth context */
98 ac
= ctx
->auth_context
;
99 if (krb5_ret_uint32 (sp
, &ac
->flags
) != 0)
101 if (flags
& SC_LOCAL_ADDRESS
) {
102 if (krb5_ret_address (sp
, localp
= &local
) != 0)
106 if (flags
& SC_REMOTE_ADDRESS
) {
107 if (krb5_ret_address (sp
, remotep
= &remote
) != 0)
111 krb5_auth_con_setaddrs (_gsskrb5_context
, ac
, localp
, remotep
);
113 krb5_free_address (_gsskrb5_context
, localp
);
115 krb5_free_address (_gsskrb5_context
, remotep
);
116 localp
= remotep
= NULL
;
118 if (krb5_ret_int16 (sp
, &ac
->local_port
) != 0)
121 if (krb5_ret_int16 (sp
, &ac
->remote_port
) != 0)
123 if (flags
& SC_KEYBLOCK
) {
124 if (krb5_ret_keyblock (sp
, &keyblock
) != 0)
126 krb5_auth_con_setkey (_gsskrb5_context
, ac
, &keyblock
);
127 krb5_free_keyblock_contents (_gsskrb5_context
, &keyblock
);
129 if (flags
& SC_LOCAL_SUBKEY
) {
130 if (krb5_ret_keyblock (sp
, &keyblock
) != 0)
132 krb5_auth_con_setlocalsubkey (_gsskrb5_context
, ac
, &keyblock
);
133 krb5_free_keyblock_contents (_gsskrb5_context
, &keyblock
);
135 if (flags
& SC_REMOTE_SUBKEY
) {
136 if (krb5_ret_keyblock (sp
, &keyblock
) != 0)
138 krb5_auth_con_setremotesubkey (_gsskrb5_context
, ac
, &keyblock
);
139 krb5_free_keyblock_contents (_gsskrb5_context
, &keyblock
);
141 if (krb5_ret_uint32 (sp
, &ac
->local_seqnumber
))
143 if (krb5_ret_uint32 (sp
, &ac
->remote_seqnumber
))
146 if (krb5_ret_int32 (sp
, &tmp
) != 0)
149 if (krb5_ret_int32 (sp
, &tmp
) != 0)
155 if (krb5_ret_data (sp
, &data
))
157 buffer
.value
= data
.data
;
158 buffer
.length
= data
.length
;
160 ret
= _gsskrb5_import_name (minor_status
, &buffer
, GSS_C_NT_EXPORT_NAME
,
163 ret
= _gsskrb5_import_name (minor_status
, &buffer
, GSS_C_NO_OID
,
166 krb5_data_free (&data
);
170 ctx
->source
= (krb5_principal
)name
;
171 krb5_data_free (&data
);
173 if (krb5_ret_data (sp
, &data
) != 0)
175 buffer
.value
= data
.data
;
176 buffer
.length
= data
.length
;
178 ret
= _gsskrb5_import_name (minor_status
, &buffer
, GSS_C_NT_EXPORT_NAME
,
181 ret
= _gsskrb5_import_name (minor_status
, &buffer
, GSS_C_NO_OID
,
184 krb5_data_free (&data
);
188 ctx
->target
= (krb5_principal
)name
;
189 krb5_data_free (&data
);
191 if (krb5_ret_int32 (sp
, &tmp
))
194 if (krb5_ret_int32 (sp
, &tmp
))
196 ctx
->more_flags
= tmp
;
197 if (krb5_ret_int32 (sp
, &tmp
))
201 ret
= _gssapi_msg_order_import(minor_status
, sp
, &ctx
->order
);
205 krb5_storage_free (sp
);
207 *context_handle
= (gss_ctx_id_t
)ctx
;
209 return GSS_S_COMPLETE
;
212 krb5_auth_con_free (_gsskrb5_context
,
214 if (ctx
->source
!= NULL
)
215 krb5_free_principal(_gsskrb5_context
, ctx
->source
);
216 if (ctx
->target
!= NULL
)
217 krb5_free_principal(_gsskrb5_context
, ctx
->target
);
219 krb5_free_address (_gsskrb5_context
, localp
);
221 krb5_free_address (_gsskrb5_context
, remotep
);
223 _gssapi_msg_order_destroy(&ctx
->order
);
224 HEIMDAL_MUTEX_destroy(&ctx
->ctx_id_mutex
);
225 krb5_storage_free (sp
);
227 *context_handle
= GSS_C_NO_CONTEXT
;