Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / heimdal / lib / gssapi / import_sec_context.c
1 /*
2  * Copyright (c) 1999 - 2001 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.5 2001/05/11 09:16:46 assar 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     sp = krb5_storage_from_mem (interprocess_token->value,
61                                 interprocess_token->length);
62     if (sp == NULL) {
63         *minor_status = ENOMEM;
64         return GSS_S_FAILURE;
65     }
66
67     *context_handle = malloc(sizeof(**context_handle));
68     if (*context_handle == NULL) {
69         *minor_status = ENOMEM;
70         krb5_storage_free (sp);
71         return GSS_S_FAILURE;
72     }
73     memset (*context_handle, 0, sizeof(**context_handle));
74
75     kret = krb5_auth_con_init (gssapi_krb5_context,
76                                &(*context_handle)->auth_context);
77     if (kret) {
78         gssapi_krb5_set_error_string ();
79         *minor_status = kret;
80         ret = GSS_S_FAILURE;
81         goto failure;
82     }
83
84     /* flags */
85
86     krb5_ret_int32 (sp, &flags);
87
88     /* retrieve the auth context */
89
90     ac = (*context_handle)->auth_context;
91     krb5_ret_int32 (sp, &ac->flags);
92     if (flags & SC_LOCAL_ADDRESS)
93         krb5_ret_address (sp, localp = &local);
94     else
95         localp = NULL;
96     if (flags & SC_REMOTE_ADDRESS)
97         krb5_ret_address (sp, remotep  = &remote);
98     else
99         remotep = NULL;
100     krb5_auth_con_setaddrs (gssapi_krb5_context, ac, localp, remotep);
101     if (localp)
102         krb5_free_address (gssapi_krb5_context, localp);
103     if (remotep)
104         krb5_free_address (gssapi_krb5_context, remotep);
105     krb5_ret_int16 (sp, &ac->local_port);
106     krb5_ret_int16 (sp, &ac->remote_port);
107     if (flags & SC_KEYBLOCK) {
108         krb5_ret_keyblock (sp, &keyblock);
109         krb5_auth_con_setkey (gssapi_krb5_context, ac, &keyblock);
110         krb5_free_keyblock_contents (gssapi_krb5_context, &keyblock);
111     }
112     if (flags & SC_LOCAL_SUBKEY) {
113         krb5_ret_keyblock (sp, &keyblock);
114         krb5_auth_con_setlocalsubkey (gssapi_krb5_context, ac, &keyblock);
115         krb5_free_keyblock_contents (gssapi_krb5_context, &keyblock);
116     }
117     if (flags & SC_REMOTE_SUBKEY) {
118         krb5_ret_keyblock (sp, &keyblock);
119         krb5_auth_con_setremotesubkey (gssapi_krb5_context, ac, &keyblock);
120         krb5_free_keyblock_contents (gssapi_krb5_context, &keyblock);
121     }
122     krb5_ret_int32 (sp, &ac->local_seqnumber);
123     krb5_ret_int32 (sp, &ac->remote_seqnumber);
124
125 #if 0
126     {
127             size_t sz;
128
129             krb5_ret_data (sp, &data);
130             ac->authenticator = malloc (sizeof (*ac->authenticator));
131             if (ac->authenticator == NULL) {
132                 *minor_status = ENOMEM;
133                 ret = GSS_S_FAILURE;
134                 goto failure;
135             }
136
137             kret = decode_Authenticator (data.data, data.length,
138                                          ac->authenticator, &sz);
139             krb5_data_free (&data);
140             if (kret) {
141                 *minor_status = kret;
142                 ret = GSS_S_FAILURE;
143                 goto failure;
144             }
145     }
146 #endif
147
148     krb5_ret_int32 (sp, &tmp);
149     ac->keytype = tmp;
150     krb5_ret_int32 (sp, &tmp);
151     ac->cksumtype = tmp;
152
153     /* names */
154
155     krb5_ret_data (sp, &data);
156     buffer.value  = data.data;
157     buffer.length = data.length;
158
159     ret = gss_import_name (minor_status, &buffer, GSS_C_NO_OID,
160                            &(*context_handle)->source);
161     krb5_data_free (&data);
162     if (ret)
163         goto failure;
164
165     krb5_ret_data (sp, &data);
166     buffer.value  = data.data;
167     buffer.length = data.length;
168
169     ret = gss_import_name (minor_status, &buffer, GSS_C_NO_OID,
170                            &(*context_handle)->target);
171     krb5_data_free (&data);
172     if (ret)
173         goto failure;
174
175     krb5_ret_int32 (sp, &tmp);
176     (*context_handle)->flags = tmp;
177     krb5_ret_int32 (sp, &tmp);
178     (*context_handle)->more_flags = tmp;
179
180     return GSS_S_COMPLETE;
181
182 failure:
183     krb5_auth_con_free (gssapi_krb5_context,
184                         (*context_handle)->auth_context);
185     if ((*context_handle)->source != NULL)
186         gss_release_name(&minor, &(*context_handle)->source);
187     if ((*context_handle)->target != NULL)
188         gss_release_name(&minor, &(*context_handle)->target);
189     free (*context_handle);
190     *context_handle = GSS_C_NO_CONTEXT;
191     return ret;
192 }