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 "gssapi_locl.h"
36 RCSID("$Id: import_sec_context.c,v 1.7 2003/03/16 18:01:32 lha Exp $");
39 gss_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
;
60 localp
= remotep
= NULL
;
62 sp
= krb5_storage_from_mem (interprocess_token
->value
,
63 interprocess_token
->length
);
65 *minor_status
= ENOMEM
;
69 *context_handle
= malloc(sizeof(**context_handle
));
70 if (*context_handle
== NULL
) {
71 *minor_status
= ENOMEM
;
72 krb5_storage_free (sp
);
75 memset (*context_handle
, 0, sizeof(**context_handle
));
77 kret
= krb5_auth_con_init (gssapi_krb5_context
,
78 &(*context_handle
)->auth_context
);
80 gssapi_krb5_set_error_string ();
90 if (krb5_ret_int32 (sp
, &flags
) != 0)
93 /* retrieve the auth context */
95 ac
= (*context_handle
)->auth_context
;
96 krb5_ret_int32 (sp
, &ac
->flags
);
97 if (flags
& SC_LOCAL_ADDRESS
) {
98 if (krb5_ret_address (sp
, localp
= &local
) != 0)
102 if (flags
& SC_REMOTE_ADDRESS
) {
103 if (krb5_ret_address (sp
, remotep
= &remote
) != 0)
107 krb5_auth_con_setaddrs (gssapi_krb5_context
, ac
, localp
, remotep
);
109 krb5_free_address (gssapi_krb5_context
, localp
);
111 krb5_free_address (gssapi_krb5_context
, remotep
);
112 localp
= remotep
= NULL
;
114 if (krb5_ret_int16 (sp
, &ac
->local_port
) != 0)
117 if (krb5_ret_int16 (sp
, &ac
->remote_port
) != 0)
119 if (flags
& SC_KEYBLOCK
) {
120 if (krb5_ret_keyblock (sp
, &keyblock
) != 0)
122 krb5_auth_con_setkey (gssapi_krb5_context
, ac
, &keyblock
);
123 krb5_free_keyblock_contents (gssapi_krb5_context
, &keyblock
);
125 if (flags
& SC_LOCAL_SUBKEY
) {
126 if (krb5_ret_keyblock (sp
, &keyblock
) != 0)
128 krb5_auth_con_setlocalsubkey (gssapi_krb5_context
, ac
, &keyblock
);
129 krb5_free_keyblock_contents (gssapi_krb5_context
, &keyblock
);
131 if (flags
& SC_REMOTE_SUBKEY
) {
132 if (krb5_ret_keyblock (sp
, &keyblock
) != 0)
134 krb5_auth_con_setremotesubkey (gssapi_krb5_context
, ac
, &keyblock
);
135 krb5_free_keyblock_contents (gssapi_krb5_context
, &keyblock
);
137 if (krb5_ret_int32 (sp
, &ac
->local_seqnumber
))
139 if (krb5_ret_int32 (sp
, &ac
->remote_seqnumber
))
142 if (krb5_ret_int32 (sp
, &tmp
) != 0)
145 if (krb5_ret_int32 (sp
, &tmp
) != 0)
151 if (krb5_ret_data (sp
, &data
))
153 buffer
.value
= data
.data
;
154 buffer
.length
= data
.length
;
156 ret
= gss_import_name (minor_status
, &buffer
, GSS_C_NT_EXPORT_NAME
,
157 &(*context_handle
)->source
);
159 ret
= gss_import_name (minor_status
, &buffer
, GSS_C_NO_OID
,
160 &(*context_handle
)->source
);
162 krb5_data_free (&data
);
166 krb5_data_free (&data
);
168 if (krb5_ret_data (sp
, &data
) != 0)
170 buffer
.value
= data
.data
;
171 buffer
.length
= data
.length
;
173 ret
= gss_import_name (minor_status
, &buffer
, GSS_C_NT_EXPORT_NAME
,
174 &(*context_handle
)->target
);
176 ret
= gss_import_name (minor_status
, &buffer
, GSS_C_NO_OID
,
177 &(*context_handle
)->target
);
179 krb5_data_free (&data
);
183 krb5_data_free (&data
);
185 if (krb5_ret_int32 (sp
, &tmp
))
187 (*context_handle
)->flags
= tmp
;
188 if (krb5_ret_int32 (sp
, &tmp
))
190 (*context_handle
)->more_flags
= tmp
;
191 if (krb5_ret_int32 (sp
, &tmp
) == 0)
192 (*context_handle
)->lifetime
= tmp
;
194 (*context_handle
)->lifetime
= GSS_C_INDEFINITE
;
196 return GSS_S_COMPLETE
;
199 krb5_auth_con_free (gssapi_krb5_context
,
200 (*context_handle
)->auth_context
);
201 if ((*context_handle
)->source
!= NULL
)
202 gss_release_name(&minor
, &(*context_handle
)->source
);
203 if ((*context_handle
)->target
!= NULL
)
204 gss_release_name(&minor
, &(*context_handle
)->target
);
206 krb5_free_address (gssapi_krb5_context
, localp
);
208 krb5_free_address (gssapi_krb5_context
, remotep
);
209 free (*context_handle
);
210 *context_handle
= GSS_C_NO_CONTEXT
;