Restructure Makefiles to accomodate multiple archs
[dragonfly.git] / crypto / heimdal-0.6.3 / lib / gssapi / import_sec_context.c
1 /*
2  * Copyright (c) 1999 - 2003 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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. 
20  *
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 
31  * SUCH DAMAGE. 
32  */
33
34 #include "gssapi_locl.h"
35
36 RCSID("$Id: import_sec_context.c,v 1.7 2003/03/16 18:01:32 lha Exp $");
37
38 OM_uint32
39 gss_import_sec_context (
40     OM_uint32 * minor_status,
41     const gss_buffer_t interprocess_token,
42     gss_ctx_id_t * context_handle
43     )
44 {
45     OM_uint32 ret = GSS_S_FAILURE;
46     krb5_error_code kret;
47     krb5_storage *sp;
48     krb5_auth_context ac;
49     krb5_address local, remote;
50     krb5_address *localp, *remotep;
51     krb5_data data;
52     gss_buffer_desc buffer;
53     krb5_keyblock keyblock;
54     int32_t tmp;
55     int32_t flags;
56     OM_uint32 minor;
57
58     GSSAPI_KRB5_INIT ();
59
60     localp = remotep = NULL;
61
62     sp = krb5_storage_from_mem (interprocess_token->value,
63                                 interprocess_token->length);
64     if (sp == NULL) {
65         *minor_status = ENOMEM;
66         return GSS_S_FAILURE;
67     }
68
69     *context_handle = malloc(sizeof(**context_handle));
70     if (*context_handle == NULL) {
71         *minor_status = ENOMEM;
72         krb5_storage_free (sp);
73         return GSS_S_FAILURE;
74     }
75     memset (*context_handle, 0, sizeof(**context_handle));
76
77     kret = krb5_auth_con_init (gssapi_krb5_context,
78                                &(*context_handle)->auth_context);
79     if (kret) {
80         gssapi_krb5_set_error_string ();
81         *minor_status = kret;
82         ret = GSS_S_FAILURE;
83         goto failure;
84     }
85
86     /* flags */
87
88     *minor_status = 0;
89
90     if (krb5_ret_int32 (sp, &flags) != 0)
91         goto failure;
92
93     /* retrieve the auth context */
94
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)
99             goto failure;
100     }
101
102     if (flags & SC_REMOTE_ADDRESS) {
103         if (krb5_ret_address (sp, remotep = &remote) != 0)
104             goto failure;
105     }
106
107     krb5_auth_con_setaddrs (gssapi_krb5_context, ac, localp, remotep);
108     if (localp)
109         krb5_free_address (gssapi_krb5_context, localp);
110     if (remotep)
111         krb5_free_address (gssapi_krb5_context, remotep);
112     localp = remotep = NULL;
113
114     if (krb5_ret_int16 (sp, &ac->local_port) != 0)
115         goto failure;
116
117     if (krb5_ret_int16 (sp, &ac->remote_port) != 0)
118         goto failure;
119     if (flags & SC_KEYBLOCK) {
120         if (krb5_ret_keyblock (sp, &keyblock) != 0)
121             goto failure;
122         krb5_auth_con_setkey (gssapi_krb5_context, ac, &keyblock);
123         krb5_free_keyblock_contents (gssapi_krb5_context, &keyblock);
124     }
125     if (flags & SC_LOCAL_SUBKEY) {
126         if (krb5_ret_keyblock (sp, &keyblock) != 0)
127             goto failure;
128         krb5_auth_con_setlocalsubkey (gssapi_krb5_context, ac, &keyblock);
129         krb5_free_keyblock_contents (gssapi_krb5_context, &keyblock);
130     }
131     if (flags & SC_REMOTE_SUBKEY) {
132         if (krb5_ret_keyblock (sp, &keyblock) != 0)
133             goto failure;
134         krb5_auth_con_setremotesubkey (gssapi_krb5_context, ac, &keyblock);
135         krb5_free_keyblock_contents (gssapi_krb5_context, &keyblock);
136     }
137     if (krb5_ret_int32 (sp, &ac->local_seqnumber))
138         goto failure;
139     if (krb5_ret_int32 (sp, &ac->remote_seqnumber))
140         goto failure;
141
142     if (krb5_ret_int32 (sp, &tmp) != 0)
143         goto failure;
144     ac->keytype = tmp;
145     if (krb5_ret_int32 (sp, &tmp) != 0)
146         goto failure;
147     ac->cksumtype = tmp;
148
149     /* names */
150
151     if (krb5_ret_data (sp, &data))
152         goto failure;
153     buffer.value  = data.data;
154     buffer.length = data.length;
155
156     ret = gss_import_name (minor_status, &buffer, GSS_C_NT_EXPORT_NAME,
157                            &(*context_handle)->source);
158     if (ret) {
159         ret = gss_import_name (minor_status, &buffer, GSS_C_NO_OID,
160                                &(*context_handle)->source);
161         if (ret) {
162             krb5_data_free (&data);
163             goto failure;
164         }
165     }
166     krb5_data_free (&data);
167
168     if (krb5_ret_data (sp, &data) != 0)
169         goto failure;
170     buffer.value  = data.data;
171     buffer.length = data.length;
172
173     ret = gss_import_name (minor_status, &buffer, GSS_C_NT_EXPORT_NAME,
174                            &(*context_handle)->target);
175     if (ret) {
176         ret = gss_import_name (minor_status, &buffer, GSS_C_NO_OID,
177                                &(*context_handle)->target);
178         if (ret) {
179             krb5_data_free (&data);
180             goto failure;
181         }
182     }    
183     krb5_data_free (&data);
184
185     if (krb5_ret_int32 (sp, &tmp))
186         goto failure;
187     (*context_handle)->flags = tmp;
188     if (krb5_ret_int32 (sp, &tmp))
189         goto failure;
190     (*context_handle)->more_flags = tmp;
191     if (krb5_ret_int32 (sp, &tmp) == 0)
192         (*context_handle)->lifetime = tmp;
193     else
194         (*context_handle)->lifetime = GSS_C_INDEFINITE;
195
196     return GSS_S_COMPLETE;
197
198 failure:
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);
205     if (localp)
206         krb5_free_address (gssapi_krb5_context, localp);
207     if (remotep)
208         krb5_free_address (gssapi_krb5_context, remotep);
209     free (*context_handle);
210     *context_handle = GSS_C_NO_CONTEXT;
211     return ret;
212 }