1*0a6a1f1dSLionel Sambuc /* $NetBSD: creds.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2009 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "gsskrb5_locl.h"
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV
_gsskrb5_export_cred(OM_uint32 * minor_status,gss_cred_id_t cred_handle,gss_buffer_t cred_token)39ebfedea0SLionel Sambuc _gsskrb5_export_cred(OM_uint32 *minor_status,
40ebfedea0SLionel Sambuc gss_cred_id_t cred_handle,
41ebfedea0SLionel Sambuc gss_buffer_t cred_token)
42ebfedea0SLionel Sambuc {
43ebfedea0SLionel Sambuc gsskrb5_cred handle = (gsskrb5_cred)cred_handle;
44ebfedea0SLionel Sambuc krb5_context context;
45ebfedea0SLionel Sambuc krb5_error_code ret;
46ebfedea0SLionel Sambuc krb5_storage *sp;
47ebfedea0SLionel Sambuc krb5_data data, mech;
48ebfedea0SLionel Sambuc const char *type;
49ebfedea0SLionel Sambuc char *str;
50ebfedea0SLionel Sambuc
51ebfedea0SLionel Sambuc GSSAPI_KRB5_INIT (&context);
52ebfedea0SLionel Sambuc
53ebfedea0SLionel Sambuc if (handle->usage != GSS_C_INITIATE && handle->usage != GSS_C_BOTH) {
54ebfedea0SLionel Sambuc *minor_status = GSS_KRB5_S_G_BAD_USAGE;
55ebfedea0SLionel Sambuc return GSS_S_FAILURE;
56ebfedea0SLionel Sambuc }
57ebfedea0SLionel Sambuc
58ebfedea0SLionel Sambuc sp = krb5_storage_emem();
59ebfedea0SLionel Sambuc if (sp == NULL) {
60ebfedea0SLionel Sambuc *minor_status = ENOMEM;
61ebfedea0SLionel Sambuc return GSS_S_FAILURE;
62ebfedea0SLionel Sambuc }
63ebfedea0SLionel Sambuc
64ebfedea0SLionel Sambuc type = krb5_cc_get_type(context, handle->ccache);
65ebfedea0SLionel Sambuc if (strcmp(type, "MEMORY") == 0) {
66ebfedea0SLionel Sambuc krb5_creds *creds;
67ebfedea0SLionel Sambuc ret = krb5_store_uint32(sp, 0);
68ebfedea0SLionel Sambuc if (ret) {
69ebfedea0SLionel Sambuc krb5_storage_free(sp);
70ebfedea0SLionel Sambuc *minor_status = ret;
71ebfedea0SLionel Sambuc return GSS_S_FAILURE;
72ebfedea0SLionel Sambuc }
73ebfedea0SLionel Sambuc
74ebfedea0SLionel Sambuc ret = _krb5_get_krbtgt(context, handle->ccache,
75ebfedea0SLionel Sambuc handle->principal->realm,
76ebfedea0SLionel Sambuc &creds);
77ebfedea0SLionel Sambuc if (ret) {
78ebfedea0SLionel Sambuc krb5_storage_free(sp);
79ebfedea0SLionel Sambuc *minor_status = ret;
80ebfedea0SLionel Sambuc return GSS_S_FAILURE;
81ebfedea0SLionel Sambuc }
82ebfedea0SLionel Sambuc
83ebfedea0SLionel Sambuc ret = krb5_store_creds(sp, creds);
84ebfedea0SLionel Sambuc krb5_free_creds(context, creds);
85ebfedea0SLionel Sambuc if (ret) {
86ebfedea0SLionel Sambuc krb5_storage_free(sp);
87ebfedea0SLionel Sambuc *minor_status = ret;
88ebfedea0SLionel Sambuc return GSS_S_FAILURE;
89ebfedea0SLionel Sambuc }
90ebfedea0SLionel Sambuc
91ebfedea0SLionel Sambuc } else {
92ebfedea0SLionel Sambuc ret = krb5_store_uint32(sp, 1);
93ebfedea0SLionel Sambuc if (ret) {
94ebfedea0SLionel Sambuc krb5_storage_free(sp);
95ebfedea0SLionel Sambuc *minor_status = ret;
96ebfedea0SLionel Sambuc return GSS_S_FAILURE;
97ebfedea0SLionel Sambuc }
98ebfedea0SLionel Sambuc
99ebfedea0SLionel Sambuc ret = krb5_cc_get_full_name(context, handle->ccache, &str);
100ebfedea0SLionel Sambuc if (ret) {
101ebfedea0SLionel Sambuc krb5_storage_free(sp);
102ebfedea0SLionel Sambuc *minor_status = ret;
103ebfedea0SLionel Sambuc return GSS_S_FAILURE;
104ebfedea0SLionel Sambuc }
105ebfedea0SLionel Sambuc
106ebfedea0SLionel Sambuc ret = krb5_store_string(sp, str);
107ebfedea0SLionel Sambuc free(str);
108ebfedea0SLionel Sambuc if (ret) {
109ebfedea0SLionel Sambuc krb5_storage_free(sp);
110ebfedea0SLionel Sambuc *minor_status = ret;
111ebfedea0SLionel Sambuc return GSS_S_FAILURE;
112ebfedea0SLionel Sambuc }
113ebfedea0SLionel Sambuc }
114ebfedea0SLionel Sambuc ret = krb5_storage_to_data(sp, &data);
115ebfedea0SLionel Sambuc krb5_storage_free(sp);
116ebfedea0SLionel Sambuc if (ret) {
117ebfedea0SLionel Sambuc *minor_status = ret;
118ebfedea0SLionel Sambuc return GSS_S_FAILURE;
119ebfedea0SLionel Sambuc }
120ebfedea0SLionel Sambuc sp = krb5_storage_emem();
121ebfedea0SLionel Sambuc if (sp == NULL) {
122ebfedea0SLionel Sambuc krb5_data_free(&data);
123ebfedea0SLionel Sambuc *minor_status = ENOMEM;
124ebfedea0SLionel Sambuc return GSS_S_FAILURE;
125ebfedea0SLionel Sambuc }
126ebfedea0SLionel Sambuc
127ebfedea0SLionel Sambuc mech.data = GSS_KRB5_MECHANISM->elements;
128ebfedea0SLionel Sambuc mech.length = GSS_KRB5_MECHANISM->length;
129ebfedea0SLionel Sambuc
130ebfedea0SLionel Sambuc ret = krb5_store_data(sp, mech);
131ebfedea0SLionel Sambuc if (ret) {
132ebfedea0SLionel Sambuc krb5_data_free(&data);
133ebfedea0SLionel Sambuc krb5_storage_free(sp);
134ebfedea0SLionel Sambuc *minor_status = ret;
135ebfedea0SLionel Sambuc return GSS_S_FAILURE;
136ebfedea0SLionel Sambuc }
137ebfedea0SLionel Sambuc
138ebfedea0SLionel Sambuc ret = krb5_store_data(sp, data);
139ebfedea0SLionel Sambuc krb5_data_free(&data);
140ebfedea0SLionel Sambuc if (ret) {
141ebfedea0SLionel Sambuc krb5_storage_free(sp);
142ebfedea0SLionel Sambuc *minor_status = ret;
143ebfedea0SLionel Sambuc return GSS_S_FAILURE;
144ebfedea0SLionel Sambuc }
145ebfedea0SLionel Sambuc
146ebfedea0SLionel Sambuc ret = krb5_storage_to_data(sp, &data);
147ebfedea0SLionel Sambuc krb5_storage_free(sp);
148ebfedea0SLionel Sambuc if (ret) {
149ebfedea0SLionel Sambuc *minor_status = ret;
150ebfedea0SLionel Sambuc return GSS_S_FAILURE;
151ebfedea0SLionel Sambuc }
152ebfedea0SLionel Sambuc
153ebfedea0SLionel Sambuc cred_token->value = data.data;
154ebfedea0SLionel Sambuc cred_token->length = data.length;
155ebfedea0SLionel Sambuc
156ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
157ebfedea0SLionel Sambuc }
158ebfedea0SLionel Sambuc
159ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV
_gsskrb5_import_cred(OM_uint32 * minor_status,gss_buffer_t cred_token,gss_cred_id_t * cred_handle)160ebfedea0SLionel Sambuc _gsskrb5_import_cred(OM_uint32 * minor_status,
161ebfedea0SLionel Sambuc gss_buffer_t cred_token,
162ebfedea0SLionel Sambuc gss_cred_id_t * cred_handle)
163ebfedea0SLionel Sambuc {
164ebfedea0SLionel Sambuc krb5_context context;
165ebfedea0SLionel Sambuc krb5_error_code ret;
166ebfedea0SLionel Sambuc gsskrb5_cred handle;
167ebfedea0SLionel Sambuc krb5_ccache id;
168ebfedea0SLionel Sambuc krb5_storage *sp;
169ebfedea0SLionel Sambuc char *str;
170ebfedea0SLionel Sambuc uint32_t type;
171ebfedea0SLionel Sambuc int flags = 0;
172ebfedea0SLionel Sambuc
173ebfedea0SLionel Sambuc *cred_handle = GSS_C_NO_CREDENTIAL;
174ebfedea0SLionel Sambuc
175ebfedea0SLionel Sambuc GSSAPI_KRB5_INIT (&context);
176ebfedea0SLionel Sambuc
177ebfedea0SLionel Sambuc sp = krb5_storage_from_mem(cred_token->value, cred_token->length);
178ebfedea0SLionel Sambuc if (sp == NULL) {
179ebfedea0SLionel Sambuc *minor_status = ENOMEM;
180ebfedea0SLionel Sambuc return GSS_S_FAILURE;
181ebfedea0SLionel Sambuc }
182ebfedea0SLionel Sambuc
183ebfedea0SLionel Sambuc ret = krb5_ret_uint32(sp, &type);
184ebfedea0SLionel Sambuc if (ret) {
185ebfedea0SLionel Sambuc krb5_storage_free(sp);
186ebfedea0SLionel Sambuc *minor_status = ret;
187ebfedea0SLionel Sambuc return GSS_S_FAILURE;
188ebfedea0SLionel Sambuc }
189ebfedea0SLionel Sambuc switch (type) {
190ebfedea0SLionel Sambuc case 0: {
191ebfedea0SLionel Sambuc krb5_creds creds;
192ebfedea0SLionel Sambuc
193ebfedea0SLionel Sambuc ret = krb5_ret_creds(sp, &creds);
194ebfedea0SLionel Sambuc krb5_storage_free(sp);
195ebfedea0SLionel Sambuc if (ret) {
196ebfedea0SLionel Sambuc *minor_status = ret;
197ebfedea0SLionel Sambuc return GSS_S_FAILURE;
198ebfedea0SLionel Sambuc }
199ebfedea0SLionel Sambuc
200ebfedea0SLionel Sambuc ret = krb5_cc_new_unique(context, "MEMORY", NULL, &id);
201ebfedea0SLionel Sambuc if (ret) {
202ebfedea0SLionel Sambuc *minor_status = ret;
203ebfedea0SLionel Sambuc return GSS_S_FAILURE;
204ebfedea0SLionel Sambuc }
205ebfedea0SLionel Sambuc
206ebfedea0SLionel Sambuc ret = krb5_cc_initialize(context, id, creds.client);
207ebfedea0SLionel Sambuc if (ret) {
208ebfedea0SLionel Sambuc krb5_cc_destroy(context, id);
209ebfedea0SLionel Sambuc *minor_status = ret;
210ebfedea0SLionel Sambuc return GSS_S_FAILURE;
211ebfedea0SLionel Sambuc }
212ebfedea0SLionel Sambuc
213ebfedea0SLionel Sambuc ret = krb5_cc_store_cred(context, id, &creds);
214ebfedea0SLionel Sambuc krb5_free_cred_contents(context, &creds);
215ebfedea0SLionel Sambuc
216ebfedea0SLionel Sambuc flags |= GSS_CF_DESTROY_CRED_ON_RELEASE;
217ebfedea0SLionel Sambuc
218ebfedea0SLionel Sambuc break;
219ebfedea0SLionel Sambuc }
220ebfedea0SLionel Sambuc case 1:
221ebfedea0SLionel Sambuc ret = krb5_ret_string(sp, &str);
222ebfedea0SLionel Sambuc krb5_storage_free(sp);
223ebfedea0SLionel Sambuc if (ret) {
224ebfedea0SLionel Sambuc *minor_status = ret;
225ebfedea0SLionel Sambuc return GSS_S_FAILURE;
226ebfedea0SLionel Sambuc }
227ebfedea0SLionel Sambuc
228ebfedea0SLionel Sambuc ret = krb5_cc_resolve(context, str, &id);
229ebfedea0SLionel Sambuc krb5_xfree(str);
230ebfedea0SLionel Sambuc if (ret) {
231ebfedea0SLionel Sambuc *minor_status = ret;
232ebfedea0SLionel Sambuc return GSS_S_FAILURE;
233ebfedea0SLionel Sambuc }
234ebfedea0SLionel Sambuc break;
235ebfedea0SLionel Sambuc
236ebfedea0SLionel Sambuc default:
237ebfedea0SLionel Sambuc krb5_storage_free(sp);
238ebfedea0SLionel Sambuc *minor_status = 0;
239ebfedea0SLionel Sambuc return GSS_S_NO_CRED;
240ebfedea0SLionel Sambuc }
241ebfedea0SLionel Sambuc
242ebfedea0SLionel Sambuc handle = calloc(1, sizeof(*handle));
243ebfedea0SLionel Sambuc if (handle == NULL) {
244ebfedea0SLionel Sambuc krb5_cc_close(context, id);
245ebfedea0SLionel Sambuc *minor_status = ENOMEM;
246ebfedea0SLionel Sambuc return GSS_S_FAILURE;
247ebfedea0SLionel Sambuc }
248ebfedea0SLionel Sambuc
249ebfedea0SLionel Sambuc handle->usage = GSS_C_INITIATE;
250ebfedea0SLionel Sambuc krb5_cc_get_principal(context, id, &handle->principal);
251ebfedea0SLionel Sambuc handle->ccache = id;
252ebfedea0SLionel Sambuc handle->cred_flags = flags;
253ebfedea0SLionel Sambuc
254ebfedea0SLionel Sambuc *cred_handle = (gss_cred_id_t)handle;
255ebfedea0SLionel Sambuc
256ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
257ebfedea0SLionel Sambuc }
258