1 /* $NetBSD: creds.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
4 * Copyright (c) 2009 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "gsskrb5_locl.h"
38 OM_uint32 GSSAPI_CALLCONV
39 _gsskrb5_export_cred(OM_uint32
*minor_status
,
40 gss_cred_id_t cred_handle
,
41 gss_buffer_t cred_token
)
43 gsskrb5_cred handle
= (gsskrb5_cred
)cred_handle
;
51 GSSAPI_KRB5_INIT (&context
);
53 if (handle
->usage
!= GSS_C_INITIATE
&& handle
->usage
!= GSS_C_BOTH
) {
54 *minor_status
= GSS_KRB5_S_G_BAD_USAGE
;
58 sp
= krb5_storage_emem();
60 *minor_status
= ENOMEM
;
64 type
= krb5_cc_get_type(context
, handle
->ccache
);
65 if (strcmp(type
, "MEMORY") == 0) {
67 ret
= krb5_store_uint32(sp
, 0);
69 krb5_storage_free(sp
);
74 ret
= _krb5_get_krbtgt(context
, handle
->ccache
,
75 handle
->principal
->realm
,
78 krb5_storage_free(sp
);
83 ret
= krb5_store_creds(sp
, creds
);
84 krb5_free_creds(context
, creds
);
86 krb5_storage_free(sp
);
92 ret
= krb5_store_uint32(sp
, 1);
94 krb5_storage_free(sp
);
99 ret
= krb5_cc_get_full_name(context
, handle
->ccache
, &str
);
101 krb5_storage_free(sp
);
103 return GSS_S_FAILURE
;
106 ret
= krb5_store_string(sp
, str
);
109 krb5_storage_free(sp
);
111 return GSS_S_FAILURE
;
114 ret
= krb5_storage_to_data(sp
, &data
);
115 krb5_storage_free(sp
);
118 return GSS_S_FAILURE
;
120 sp
= krb5_storage_emem();
122 krb5_data_free(&data
);
123 *minor_status
= ENOMEM
;
124 return GSS_S_FAILURE
;
127 mech
.data
= GSS_KRB5_MECHANISM
->elements
;
128 mech
.length
= GSS_KRB5_MECHANISM
->length
;
130 ret
= krb5_store_data(sp
, mech
);
132 krb5_data_free(&data
);
133 krb5_storage_free(sp
);
135 return GSS_S_FAILURE
;
138 ret
= krb5_store_data(sp
, data
);
139 krb5_data_free(&data
);
141 krb5_storage_free(sp
);
143 return GSS_S_FAILURE
;
146 ret
= krb5_storage_to_data(sp
, &data
);
147 krb5_storage_free(sp
);
150 return GSS_S_FAILURE
;
153 cred_token
->value
= data
.data
;
154 cred_token
->length
= data
.length
;
156 return GSS_S_COMPLETE
;
159 OM_uint32 GSSAPI_CALLCONV
160 _gsskrb5_import_cred(OM_uint32
* minor_status
,
161 gss_buffer_t cred_token
,
162 gss_cred_id_t
* cred_handle
)
164 krb5_context context
;
173 *cred_handle
= GSS_C_NO_CREDENTIAL
;
175 GSSAPI_KRB5_INIT (&context
);
177 sp
= krb5_storage_from_mem(cred_token
->value
, cred_token
->length
);
179 *minor_status
= ENOMEM
;
180 return GSS_S_FAILURE
;
183 ret
= krb5_ret_uint32(sp
, &type
);
185 krb5_storage_free(sp
);
187 return GSS_S_FAILURE
;
193 ret
= krb5_ret_creds(sp
, &creds
);
194 krb5_storage_free(sp
);
197 return GSS_S_FAILURE
;
200 ret
= krb5_cc_new_unique(context
, "MEMORY", NULL
, &id
);
203 return GSS_S_FAILURE
;
206 ret
= krb5_cc_initialize(context
, id
, creds
.client
);
208 krb5_cc_destroy(context
, id
);
210 return GSS_S_FAILURE
;
213 ret
= krb5_cc_store_cred(context
, id
, &creds
);
214 krb5_free_cred_contents(context
, &creds
);
216 flags
|= GSS_CF_DESTROY_CRED_ON_RELEASE
;
221 ret
= krb5_ret_string(sp
, &str
);
222 krb5_storage_free(sp
);
225 return GSS_S_FAILURE
;
228 ret
= krb5_cc_resolve(context
, str
, &id
);
232 return GSS_S_FAILURE
;
237 krb5_storage_free(sp
);
239 return GSS_S_NO_CRED
;
242 handle
= calloc(1, sizeof(*handle
));
243 if (handle
== NULL
) {
244 krb5_cc_close(context
, id
);
245 *minor_status
= ENOMEM
;
246 return GSS_S_FAILURE
;
249 handle
->usage
= GSS_C_INITIATE
;
250 krb5_cc_get_principal(context
, id
, &handle
->principal
);
252 handle
->cred_flags
= flags
;
254 *cred_handle
= (gss_cred_id_t
)handle
;
256 return GSS_S_COMPLETE
;