xref: /minix3/crypto/external/bsd/heimdal/dist/lib/gssapi/krb5/add_cred.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: add_cred.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2003 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 
_gsskrb5_add_cred(OM_uint32 * minor_status,const gss_cred_id_t input_cred_handle,const gss_name_t desired_name,const gss_OID desired_mech,gss_cred_usage_t cred_usage,OM_uint32 initiator_time_req,OM_uint32 acceptor_time_req,gss_cred_id_t * output_cred_handle,gss_OID_set * actual_mechs,OM_uint32 * initiator_time_rec,OM_uint32 * acceptor_time_rec)38ebfedea0SLionel Sambuc OM_uint32 GSSAPI_CALLCONV _gsskrb5_add_cred (
39ebfedea0SLionel Sambuc      OM_uint32           *minor_status,
40ebfedea0SLionel Sambuc      const gss_cred_id_t input_cred_handle,
41ebfedea0SLionel Sambuc      const gss_name_t    desired_name,
42ebfedea0SLionel Sambuc      const gss_OID       desired_mech,
43ebfedea0SLionel Sambuc      gss_cred_usage_t    cred_usage,
44ebfedea0SLionel Sambuc      OM_uint32           initiator_time_req,
45ebfedea0SLionel Sambuc      OM_uint32           acceptor_time_req,
46ebfedea0SLionel Sambuc      gss_cred_id_t       *output_cred_handle,
47ebfedea0SLionel Sambuc      gss_OID_set         *actual_mechs,
48ebfedea0SLionel Sambuc      OM_uint32           *initiator_time_rec,
49ebfedea0SLionel Sambuc      OM_uint32           *acceptor_time_rec)
50ebfedea0SLionel Sambuc {
51ebfedea0SLionel Sambuc     krb5_context context;
52ebfedea0SLionel Sambuc     OM_uint32 ret, lifetime;
53ebfedea0SLionel Sambuc     gsskrb5_cred cred, handle;
54ebfedea0SLionel Sambuc     krb5_const_principal dname;
55ebfedea0SLionel Sambuc 
56ebfedea0SLionel Sambuc     handle = NULL;
57ebfedea0SLionel Sambuc     cred = (gsskrb5_cred)input_cred_handle;
58ebfedea0SLionel Sambuc     dname = (krb5_const_principal)desired_name;
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc     GSSAPI_KRB5_INIT (&context);
61ebfedea0SLionel Sambuc 
62ebfedea0SLionel Sambuc     if (gss_oid_equal(desired_mech, GSS_KRB5_MECHANISM) == 0) {
63ebfedea0SLionel Sambuc 	*minor_status = 0;
64ebfedea0SLionel Sambuc 	return GSS_S_BAD_MECH;
65ebfedea0SLionel Sambuc     }
66ebfedea0SLionel Sambuc 
67ebfedea0SLionel Sambuc     if (cred == NULL && output_cred_handle == NULL) {
68ebfedea0SLionel Sambuc 	*minor_status = 0;
69ebfedea0SLionel Sambuc 	return GSS_S_NO_CRED;
70ebfedea0SLionel Sambuc     }
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc     if (cred == NULL) { /* XXX standard conformance failure */
73ebfedea0SLionel Sambuc 	*minor_status = 0;
74ebfedea0SLionel Sambuc 	return GSS_S_NO_CRED;
75ebfedea0SLionel Sambuc     }
76ebfedea0SLionel Sambuc 
77ebfedea0SLionel Sambuc     /* check if requested output usage is compatible with output usage */
78ebfedea0SLionel Sambuc     if (output_cred_handle != NULL) {
79ebfedea0SLionel Sambuc 	HEIMDAL_MUTEX_lock(&cred->cred_id_mutex);
80ebfedea0SLionel Sambuc 	if (cred->usage != cred_usage && cred->usage != GSS_C_BOTH) {
81ebfedea0SLionel Sambuc 	    HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
82ebfedea0SLionel Sambuc 	    *minor_status = GSS_KRB5_S_G_BAD_USAGE;
83ebfedea0SLionel Sambuc 	    return(GSS_S_FAILURE);
84ebfedea0SLionel Sambuc 	}
85ebfedea0SLionel Sambuc     }
86ebfedea0SLionel Sambuc 
87ebfedea0SLionel Sambuc     /* check that we have the same name */
88ebfedea0SLionel Sambuc     if (dname != NULL &&
89ebfedea0SLionel Sambuc 	krb5_principal_compare(context, dname,
90ebfedea0SLionel Sambuc 			       cred->principal) != FALSE) {
91ebfedea0SLionel Sambuc 	if (output_cred_handle)
92ebfedea0SLionel Sambuc 	    HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
93ebfedea0SLionel Sambuc 	*minor_status = 0;
94ebfedea0SLionel Sambuc 	return GSS_S_BAD_NAME;
95ebfedea0SLionel Sambuc     }
96ebfedea0SLionel Sambuc 
97ebfedea0SLionel Sambuc     /* make a copy */
98ebfedea0SLionel Sambuc     if (output_cred_handle) {
99ebfedea0SLionel Sambuc 	krb5_error_code kret;
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc 	handle = calloc(1, sizeof(*handle));
102ebfedea0SLionel Sambuc 	if (handle == NULL) {
103ebfedea0SLionel Sambuc 	    HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
104ebfedea0SLionel Sambuc 	    *minor_status = ENOMEM;
105ebfedea0SLionel Sambuc 	    return (GSS_S_FAILURE);
106ebfedea0SLionel Sambuc 	}
107ebfedea0SLionel Sambuc 
108ebfedea0SLionel Sambuc 	handle->usage = cred_usage;
109ebfedea0SLionel Sambuc 	handle->lifetime = cred->lifetime;
110ebfedea0SLionel Sambuc 	handle->principal = NULL;
111ebfedea0SLionel Sambuc 	handle->keytab = NULL;
112ebfedea0SLionel Sambuc 	handle->ccache = NULL;
113ebfedea0SLionel Sambuc 	handle->mechanisms = NULL;
114ebfedea0SLionel Sambuc 	HEIMDAL_MUTEX_init(&handle->cred_id_mutex);
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc 	ret = GSS_S_FAILURE;
117ebfedea0SLionel Sambuc 
118ebfedea0SLionel Sambuc 	kret = krb5_copy_principal(context, cred->principal,
119ebfedea0SLionel Sambuc 				  &handle->principal);
120ebfedea0SLionel Sambuc 	if (kret) {
121ebfedea0SLionel Sambuc 	    HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
122ebfedea0SLionel Sambuc 	    free(handle);
123ebfedea0SLionel Sambuc 	    *minor_status = kret;
124ebfedea0SLionel Sambuc 	    return GSS_S_FAILURE;
125ebfedea0SLionel Sambuc 	}
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc 	if (cred->keytab) {
128ebfedea0SLionel Sambuc 	    char *name = NULL;
129ebfedea0SLionel Sambuc 
130ebfedea0SLionel Sambuc 	    ret = GSS_S_FAILURE;
131ebfedea0SLionel Sambuc 
132ebfedea0SLionel Sambuc 	    kret = krb5_kt_get_full_name(context, cred->keytab, &name);
133ebfedea0SLionel Sambuc 	    if (kret) {
134ebfedea0SLionel Sambuc 		*minor_status = kret;
135ebfedea0SLionel Sambuc 		goto failure;
136ebfedea0SLionel Sambuc 	    }
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc 	    kret = krb5_kt_resolve(context, name,
139ebfedea0SLionel Sambuc 				   &handle->keytab);
140ebfedea0SLionel Sambuc 	    krb5_xfree(name);
141ebfedea0SLionel Sambuc 	    if (kret){
142ebfedea0SLionel Sambuc 		*minor_status = kret;
143ebfedea0SLionel Sambuc 		goto failure;
144ebfedea0SLionel Sambuc 	    }
145ebfedea0SLionel Sambuc 	}
146ebfedea0SLionel Sambuc 
147ebfedea0SLionel Sambuc 	if (cred->ccache) {
148ebfedea0SLionel Sambuc 	    const char *type, *name;
149ebfedea0SLionel Sambuc 	    char *type_name = NULL;
150ebfedea0SLionel Sambuc 
151ebfedea0SLionel Sambuc 	    ret = GSS_S_FAILURE;
152ebfedea0SLionel Sambuc 
153ebfedea0SLionel Sambuc 	    type = krb5_cc_get_type(context, cred->ccache);
154ebfedea0SLionel Sambuc 	    if (type == NULL){
155ebfedea0SLionel Sambuc 		*minor_status = ENOMEM;
156ebfedea0SLionel Sambuc 		goto failure;
157ebfedea0SLionel Sambuc 	    }
158ebfedea0SLionel Sambuc 
159ebfedea0SLionel Sambuc 	    if (strcmp(type, "MEMORY") == 0) {
160ebfedea0SLionel Sambuc 		ret = krb5_cc_new_unique(context, type,
161ebfedea0SLionel Sambuc 					 NULL, &handle->ccache);
162ebfedea0SLionel Sambuc 		if (ret) {
163ebfedea0SLionel Sambuc 		    *minor_status = ret;
164ebfedea0SLionel Sambuc 		    goto failure;
165ebfedea0SLionel Sambuc 		}
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc 		ret = krb5_cc_copy_cache(context, cred->ccache,
168ebfedea0SLionel Sambuc 					 handle->ccache);
169ebfedea0SLionel Sambuc 		if (ret) {
170ebfedea0SLionel Sambuc 		    *minor_status = ret;
171ebfedea0SLionel Sambuc 		    goto failure;
172ebfedea0SLionel Sambuc 		}
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc 	    } else {
175ebfedea0SLionel Sambuc 		name = krb5_cc_get_name(context, cred->ccache);
176ebfedea0SLionel Sambuc 		if (name == NULL) {
177ebfedea0SLionel Sambuc 		    *minor_status = ENOMEM;
178ebfedea0SLionel Sambuc 		    goto failure;
179ebfedea0SLionel Sambuc 		}
180ebfedea0SLionel Sambuc 
181ebfedea0SLionel Sambuc 		kret = asprintf(&type_name, "%s:%s", type, name);
182ebfedea0SLionel Sambuc 		if (kret < 0 || type_name == NULL) {
183ebfedea0SLionel Sambuc 		    *minor_status = ENOMEM;
184ebfedea0SLionel Sambuc 		    goto failure;
185ebfedea0SLionel Sambuc 		}
186ebfedea0SLionel Sambuc 
187ebfedea0SLionel Sambuc 		kret = krb5_cc_resolve(context, type_name,
188ebfedea0SLionel Sambuc 				       &handle->ccache);
189ebfedea0SLionel Sambuc 		free(type_name);
190ebfedea0SLionel Sambuc 		if (kret) {
191ebfedea0SLionel Sambuc 		    *minor_status = kret;
192ebfedea0SLionel Sambuc 		    goto failure;
193ebfedea0SLionel Sambuc 		}
194ebfedea0SLionel Sambuc 	    }
195ebfedea0SLionel Sambuc 	}
196ebfedea0SLionel Sambuc 	ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
197ebfedea0SLionel Sambuc 	if (ret)
198ebfedea0SLionel Sambuc 	    goto failure;
199ebfedea0SLionel Sambuc 
200ebfedea0SLionel Sambuc 	ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
201ebfedea0SLionel Sambuc 				     &handle->mechanisms);
202ebfedea0SLionel Sambuc 	if (ret)
203ebfedea0SLionel Sambuc 	    goto failure;
204ebfedea0SLionel Sambuc     }
205ebfedea0SLionel Sambuc 
206ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
207ebfedea0SLionel Sambuc 
208ebfedea0SLionel Sambuc     ret = _gsskrb5_inquire_cred(minor_status, (gss_cred_id_t)cred,
209ebfedea0SLionel Sambuc 				NULL, &lifetime, NULL, actual_mechs);
210ebfedea0SLionel Sambuc     if (ret)
211ebfedea0SLionel Sambuc 	goto failure;
212ebfedea0SLionel Sambuc 
213ebfedea0SLionel Sambuc     if (initiator_time_rec)
214ebfedea0SLionel Sambuc 	*initiator_time_rec = lifetime;
215ebfedea0SLionel Sambuc     if (acceptor_time_rec)
216ebfedea0SLionel Sambuc 	*acceptor_time_rec = lifetime;
217ebfedea0SLionel Sambuc 
218ebfedea0SLionel Sambuc     if (output_cred_handle) {
219ebfedea0SLionel Sambuc 	*output_cred_handle = (gss_cred_id_t)handle;
220ebfedea0SLionel Sambuc     }
221ebfedea0SLionel Sambuc 
222ebfedea0SLionel Sambuc     *minor_status = 0;
223ebfedea0SLionel Sambuc     return ret;
224ebfedea0SLionel Sambuc 
225ebfedea0SLionel Sambuc  failure:
226ebfedea0SLionel Sambuc 
227ebfedea0SLionel Sambuc     if (handle) {
228ebfedea0SLionel Sambuc 	if (handle->principal)
229ebfedea0SLionel Sambuc 	    krb5_free_principal(context, handle->principal);
230ebfedea0SLionel Sambuc 	if (handle->keytab)
231ebfedea0SLionel Sambuc 	    krb5_kt_close(context, handle->keytab);
232ebfedea0SLionel Sambuc 	if (handle->ccache)
233ebfedea0SLionel Sambuc 	    krb5_cc_destroy(context, handle->ccache);
234ebfedea0SLionel Sambuc 	if (handle->mechanisms)
235ebfedea0SLionel Sambuc 	    gss_release_oid_set(NULL, &handle->mechanisms);
236ebfedea0SLionel Sambuc 	free(handle);
237ebfedea0SLionel Sambuc     }
238ebfedea0SLionel Sambuc     if (output_cred_handle)
239ebfedea0SLionel Sambuc 	HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
240ebfedea0SLionel Sambuc     return ret;
241ebfedea0SLionel Sambuc }
242