Debian Codegraph @

lib/gssapi/krb5/creds.c

raw
1 /*
2 * Copyright (c) 2009 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 "gsskrb5_locl.h"
35
36 OM_uint32 GSSAPI_CALLCONV
37 _gsskrb5_export_cred(OM_uint32 *minor_status,
38 gss_cred_id_t cred_handle,
39 gss_buffer_t cred_token)
40 {
41 gsskrb5_cred handle = (gsskrb5_cred)cred_handle;
42 krb5_context context;
43 krb5_error_code ret;
44 krb5_storage *sp;
45 krb5_data data, mech;
46 const char *type;
47 char *str;
48
49 GSSAPI_KRB5_INIT (&context);
50
51 if (handle->usage != GSS_C_INITIATE && handle->usage != GSS_C_BOTH) {
52 *minor_status = GSS_KRB5_S_G_BAD_USAGE;
53 return GSS_S_FAILURE;
54 }
55
56 sp = krb5_storage_emem();
57 if (sp == NULL) {
58 *minor_status = ENOMEM;
59 return GSS_S_FAILURE;
60 }
61
62 type = krb5_cc_get_type(context, handle->ccache);
63 if (strcmp(type, "MEMORY") == 0) {
64 krb5_creds *creds;
65 krb5_data config_start_realm;
66 char *start_realm;
67
68 ret = krb5_store_uint32(sp, 0);
69 if (ret) {
70 krb5_storage_free(sp);
71 *minor_status = ret;
72 return GSS_S_FAILURE;
73 }
74
75 ret = krb5_cc_get_config(context, handle->ccache, NULL, "start_realm",
76 &config_start_realm);
77 if (ret == 0) {
78 start_realm = strndup(config_start_realm.data,
79 config_start_realm.length);
80 krb5_data_free(&config_start_realm);
81 } else {
82 start_realm = strdup(krb5_principal_get_realm(context,
83 handle->principal));
84 }
85 if (start_realm == NULL) {
86 *minor_status = krb5_enomem(context);
87 krb5_storage_free(sp);
88 return GSS_S_FAILURE;
89 }
90
91 ret = _krb5_get_krbtgt(context, handle->ccache, start_realm, &creds);
92 free(start_realm);
93 start_realm = NULL;
94 if (ret) {
95 krb5_storage_free(sp);
96 *minor_status = ret;
97 return GSS_S_FAILURE;
98 }
99
100 ret = krb5_store_creds(sp, creds);
101 krb5_free_creds(context, creds);
102 if (ret) {
103 krb5_storage_free(sp);
104 *minor_status = ret;
105 return GSS_S_FAILURE;
106 }
107
108 } else {
109 ret = krb5_store_uint32(sp, 1);
110 if (ret) {
111 krb5_storage_free(sp);
112 *minor_status = ret;
113 return GSS_S_FAILURE;
114 }
115
116 ret = krb5_cc_get_full_name(context, handle->ccache, &str);
117 if (ret) {
118 krb5_storage_free(sp);
119 *minor_status = ret;
120 return GSS_S_FAILURE;
121 }
122
123 ret = krb5_store_string(sp, str);
124 free(str);
125 if (ret) {
126 krb5_storage_free(sp);
127 *minor_status = ret;
128 return GSS_S_FAILURE;
129 }
130 }
131 ret = krb5_storage_to_data(sp, &data);
132 krb5_storage_free(sp);
133 if (ret) {
134 *minor_status = ret;
135 return GSS_S_FAILURE;
136 }
137 sp = krb5_storage_emem();
138 if (sp == NULL) {
139 krb5_data_free(&data);
140 *minor_status = ENOMEM;
141 return GSS_S_FAILURE;
142 }
143
144 mech.data = GSS_KRB5_MECHANISM->elements;
145 mech.length = GSS_KRB5_MECHANISM->length;
146
147 ret = krb5_store_data(sp, mech);
148 if (ret) {
149 krb5_data_free(&data);
150 krb5_storage_free(sp);
151 *minor_status = ret;
152 return GSS_S_FAILURE;
153 }
154
155 ret = krb5_store_data(sp, data);
156 krb5_data_free(&data);
157 if (ret) {
158 krb5_storage_free(sp);
159 *minor_status = ret;
160 return GSS_S_FAILURE;
161 }
162
163 ret = krb5_storage_to_data(sp, &data);
164 krb5_storage_free(sp);
165 if (ret) {
166 *minor_status = ret;
167 return GSS_S_FAILURE;
168 }
169
170 cred_token->value = data.data;
171 cred_token->length = data.length;
172
173 return GSS_S_COMPLETE;
174 }
175
176 OM_uint32 GSSAPI_CALLCONV
177 _gsskrb5_import_cred(OM_uint32 * minor_status,
178 gss_buffer_t cred_token,
179 gss_cred_id_t * cred_handle)
180 {
181 krb5_context context;
182 krb5_error_code ret;
183 gsskrb5_cred handle;
184 krb5_ccache id;
185 krb5_storage *sp;
186 char *str;
187 uint32_t type;
188 int flags = 0;
189
190 *cred_handle = GSS_C_NO_CREDENTIAL;
191
192 GSSAPI_KRB5_INIT (&context);
193
194 sp = krb5_storage_from_mem(cred_token->value, cred_token->length);
195 if (sp == NULL) {
196 *minor_status = ENOMEM;
197 return GSS_S_FAILURE;
198 }
199
200 ret = krb5_ret_uint32(sp, &type);
201 if (ret) {
202 krb5_storage_free(sp);
203 *minor_status = ret;
204 return GSS_S_FAILURE;
205 }
206 switch (type) {
207 case 0: {
208 krb5_creds creds;
209
210 ret = krb5_ret_creds(sp, &creds);
211 krb5_storage_free(sp);
212 if (ret) {
213 *minor_status = ret;
214 return GSS_S_FAILURE;
215 }
216
217 ret = krb5_cc_new_unique(context, "MEMORY", NULL, &id);
218 if (ret) {
219 *minor_status = ret;
220 return GSS_S_FAILURE;
221 }
222
223 ret = krb5_cc_initialize(context, id, creds.client);
224 if (ret) {
225 krb5_cc_destroy(context, id);
226 *minor_status = ret;
227 return GSS_S_FAILURE;
228 }
229
230 ret = krb5_cc_store_cred(context, id, &creds);
231 krb5_free_cred_contents(context, &creds);
232 if (ret) {
233 *minor_status = ret;
234 return GSS_S_FAILURE;
235 }
236
237 flags |= GSS_CF_DESTROY_CRED_ON_RELEASE;
238
239 break;
240 }
241 case 1:
242 ret = krb5_ret_string(sp, &str);
243 krb5_storage_free(sp);
244 if (ret) {
245 *minor_status = ret;
246 return GSS_S_FAILURE;
247 }
248
249 ret = krb5_cc_resolve(context, str, &id);
250 krb5_xfree(str);
251 if (ret) {
252 *minor_status = ret;
253 return GSS_S_FAILURE;
254 }
255 break;
256
257 default:
258 krb5_storage_free(sp);
259 *minor_status = 0;
260 return GSS_S_NO_CRED;
261 }
262
263 handle = calloc(1, sizeof(*handle));
264 if (handle == NULL) {
265 krb5_cc_close(context, id);
266 *minor_status = ENOMEM;
267 return GSS_S_FAILURE;
268 }
269
270 handle->usage = GSS_C_INITIATE;
271 krb5_cc_get_principal(context, id, &handle->principal);
272 handle->ccache = id;
273 handle->cred_flags = flags;
274
275 *cred_handle = (gss_cred_id_t)handle;
276
277 return GSS_S_COMPLETE;
278 }