xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/gssapi/krb5/add_cred.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: add_cred.c,v 1.2 2017/01/28 21:31:46 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 2003 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include "gsskrb5_locl.h"
37ca1c9b0cSelric 
_gsskrb5_add_cred(OM_uint32 * minor_status,gss_const_cred_id_t input_cred_handle,gss_const_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)38ca1c9b0cSelric OM_uint32 GSSAPI_CALLCONV _gsskrb5_add_cred (
39ca1c9b0cSelric      OM_uint32           *minor_status,
40b9d004c6Schristos      gss_const_cred_id_t input_cred_handle,
41b9d004c6Schristos      gss_const_name_t    desired_name,
42ca1c9b0cSelric      const gss_OID       desired_mech,
43ca1c9b0cSelric      gss_cred_usage_t    cred_usage,
44ca1c9b0cSelric      OM_uint32           initiator_time_req,
45ca1c9b0cSelric      OM_uint32           acceptor_time_req,
46ca1c9b0cSelric      gss_cred_id_t       *output_cred_handle,
47ca1c9b0cSelric      gss_OID_set         *actual_mechs,
48ca1c9b0cSelric      OM_uint32           *initiator_time_rec,
49ca1c9b0cSelric      OM_uint32           *acceptor_time_rec)
50ca1c9b0cSelric {
51ca1c9b0cSelric     krb5_context context;
52b9d004c6Schristos     OM_uint32 major, lifetime;
53ca1c9b0cSelric     gsskrb5_cred cred, handle;
54ca1c9b0cSelric     krb5_const_principal dname;
55ca1c9b0cSelric 
56ca1c9b0cSelric     handle = NULL;
57ca1c9b0cSelric     cred = (gsskrb5_cred)input_cred_handle;
58ca1c9b0cSelric     dname = (krb5_const_principal)desired_name;
59ca1c9b0cSelric 
60b9d004c6Schristos     if (cred == NULL && output_cred_handle == NULL) {
61b9d004c6Schristos         *minor_status = EINVAL;
62b9d004c6Schristos         return GSS_S_CALL_INACCESSIBLE_WRITE;
63b9d004c6Schristos     }
64b9d004c6Schristos 
65ca1c9b0cSelric     GSSAPI_KRB5_INIT (&context);
66ca1c9b0cSelric 
67b9d004c6Schristos     if (desired_mech != GSS_C_NO_OID &&
68b9d004c6Schristos         gss_oid_equal(desired_mech, GSS_KRB5_MECHANISM) == 0) {
69ca1c9b0cSelric 	*minor_status = 0;
70ca1c9b0cSelric 	return GSS_S_BAD_MECH;
71ca1c9b0cSelric     }
72ca1c9b0cSelric 
73b9d004c6Schristos     if (cred == NULL) {
74b9d004c6Schristos         /*
75b9d004c6Schristos          * Acquire a credential; output_cred_handle can't be NULL, see above.
76b9d004c6Schristos          */
77b9d004c6Schristos         heim_assert(output_cred_handle != NULL,
78b9d004c6Schristos                     "internal error in _gsskrb5_add_cred()");
79ca1c9b0cSelric 
80b9d004c6Schristos         major = _gsskrb5_acquire_cred(minor_status, desired_name,
81b9d004c6Schristos                                       min(initiator_time_req,
82b9d004c6Schristos                                           acceptor_time_req),
83b9d004c6Schristos                                       GSS_C_NO_OID_SET,
84b9d004c6Schristos                                       cred_usage,
85b9d004c6Schristos                                       output_cred_handle,
86b9d004c6Schristos                                       actual_mechs, &lifetime);
87b9d004c6Schristos         if (major != GSS_S_COMPLETE)
88b9d004c6Schristos             goto failure;
89ca1c9b0cSelric 
90b9d004c6Schristos     } else {
91b9d004c6Schristos         /*
92b9d004c6Schristos          * Check that we're done or copy input to output if
93b9d004c6Schristos          * output_cred_handle != NULL.
94b9d004c6Schristos          */
95b9d004c6Schristos 
96ca1c9b0cSelric 	HEIMDAL_MUTEX_lock(&cred->cred_id_mutex);
97b9d004c6Schristos 
98b9d004c6Schristos         /* Check if requested output usage is compatible with output usage */
99ca1c9b0cSelric 	if (cred->usage != cred_usage && cred->usage != GSS_C_BOTH) {
100ca1c9b0cSelric 	    HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
101ca1c9b0cSelric 	    *minor_status = GSS_KRB5_S_G_BAD_USAGE;
102ca1c9b0cSelric 	    return(GSS_S_FAILURE);
103ca1c9b0cSelric 	}
104ca1c9b0cSelric 
105b9d004c6Schristos         /* Check that we have the same name */
106ca1c9b0cSelric         if (dname != NULL &&
107ca1c9b0cSelric             krb5_principal_compare(context, dname,
108ca1c9b0cSelric                                    cred->principal) != FALSE) {
109ca1c9b0cSelric             HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
110ca1c9b0cSelric             *minor_status = 0;
111ca1c9b0cSelric             return GSS_S_BAD_NAME;
112ca1c9b0cSelric         }
113ca1c9b0cSelric 
114b9d004c6Schristos         if (output_cred_handle == NULL) {
115b9d004c6Schristos             /*
116b9d004c6Schristos              * This case is basically useless as we implement a single
117b9d004c6Schristos              * mechanism here, so we can't add elements to the
118b9d004c6Schristos              * input_cred_handle.
119b9d004c6Schristos              */
120b9d004c6Schristos             HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
121b9d004c6Schristos             *minor_status = 0;
122b9d004c6Schristos             return GSS_S_COMPLETE;
123b9d004c6Schristos         }
124b9d004c6Schristos 
125b9d004c6Schristos         /*
126b9d004c6Schristos          * Copy input to output -- this works as if we were a
127b9d004c6Schristos          * GSS_Duplicate_cred() for one mechanism element.
128b9d004c6Schristos          */
129ca1c9b0cSelric 
130ca1c9b0cSelric 	handle = calloc(1, sizeof(*handle));
131ca1c9b0cSelric 	if (handle == NULL) {
132b9d004c6Schristos             if (cred != NULL)
133ca1c9b0cSelric                 HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
134ca1c9b0cSelric 	    *minor_status = ENOMEM;
135ca1c9b0cSelric 	    return (GSS_S_FAILURE);
136ca1c9b0cSelric 	}
137ca1c9b0cSelric 
138ca1c9b0cSelric 	handle->usage = cred_usage;
139b9d004c6Schristos 	handle->endtime = cred->endtime;
140ca1c9b0cSelric 	handle->principal = NULL;
141ca1c9b0cSelric 	handle->keytab = NULL;
142ca1c9b0cSelric 	handle->ccache = NULL;
143ca1c9b0cSelric 	handle->mechanisms = NULL;
144ca1c9b0cSelric 	HEIMDAL_MUTEX_init(&handle->cred_id_mutex);
145ca1c9b0cSelric 
146b9d004c6Schristos 	major = GSS_S_FAILURE;
147ca1c9b0cSelric 
148b9d004c6Schristos 	*minor_status = krb5_copy_principal(context, cred->principal,
149ca1c9b0cSelric                                             &handle->principal);
150b9d004c6Schristos 	if (*minor_status) {
151ca1c9b0cSelric 	    HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
152ca1c9b0cSelric 	    free(handle);
153ca1c9b0cSelric 	    return GSS_S_FAILURE;
154ca1c9b0cSelric 	}
155ca1c9b0cSelric 
156ca1c9b0cSelric 	if (cred->keytab) {
157ca1c9b0cSelric 	    char *name = NULL;
158ca1c9b0cSelric 
159b9d004c6Schristos             *minor_status = krb5_kt_get_full_name(context, cred->keytab,
160b9d004c6Schristos                                                   &name);
161b9d004c6Schristos 	    if (*minor_status)
162ca1c9b0cSelric 		goto failure;
163ca1c9b0cSelric 
164b9d004c6Schristos             *minor_status = krb5_kt_resolve(context, name, &handle->keytab);
165ca1c9b0cSelric 	    krb5_xfree(name);
166b9d004c6Schristos 	    if (*minor_status)
167ca1c9b0cSelric 		goto failure;
168ca1c9b0cSelric 	}
169ca1c9b0cSelric 
170ca1c9b0cSelric 	if (cred->ccache) {
171ca1c9b0cSelric 	    const char *type, *name;
172ca1c9b0cSelric 	    char *type_name = NULL;
173ca1c9b0cSelric 
174ca1c9b0cSelric 	    type = krb5_cc_get_type(context, cred->ccache);
175ca1c9b0cSelric 	    if (type == NULL){
176ca1c9b0cSelric 		*minor_status = ENOMEM;
177ca1c9b0cSelric 		goto failure;
178ca1c9b0cSelric 	    }
179ca1c9b0cSelric 
180ca1c9b0cSelric 	    if (strcmp(type, "MEMORY") == 0) {
181b9d004c6Schristos 		*minor_status = krb5_cc_new_unique(context, type,
182ca1c9b0cSelric                                                    NULL, &handle->ccache);
183b9d004c6Schristos 		if (*minor_status)
184ca1c9b0cSelric 		    goto failure;
185ca1c9b0cSelric 
186b9d004c6Schristos                 *minor_status = krb5_cc_copy_cache(context, cred->ccache,
187ca1c9b0cSelric                                                    handle->ccache);
188b9d004c6Schristos 		if (*minor_status)
189ca1c9b0cSelric 		    goto failure;
190ca1c9b0cSelric 
191ca1c9b0cSelric 	    } else {
192ca1c9b0cSelric 		name = krb5_cc_get_name(context, cred->ccache);
193ca1c9b0cSelric 		if (name == NULL) {
194ca1c9b0cSelric 		    *minor_status = ENOMEM;
195ca1c9b0cSelric 		    goto failure;
196ca1c9b0cSelric 		}
197ca1c9b0cSelric 
198b9d004c6Schristos 		if (asprintf(&type_name, "%s:%s", type, name) == -1 ||
199b9d004c6Schristos                     type_name == NULL) {
200ca1c9b0cSelric 		    *minor_status = ENOMEM;
201ca1c9b0cSelric 		    goto failure;
202ca1c9b0cSelric 		}
203ca1c9b0cSelric 
204b9d004c6Schristos                 *minor_status = krb5_cc_resolve(context, type_name,
205ca1c9b0cSelric                                                 &handle->ccache);
206ca1c9b0cSelric 		free(type_name);
207b9d004c6Schristos 		if (*minor_status)
208ca1c9b0cSelric 		    goto failure;
209ca1c9b0cSelric 	    }
210ca1c9b0cSelric 	}
211b9d004c6Schristos 	major = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
212b9d004c6Schristos 	if (major != GSS_S_COMPLETE)
213ca1c9b0cSelric 	    goto failure;
214ca1c9b0cSelric 
215b9d004c6Schristos 	major = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
216ca1c9b0cSelric 				       &handle->mechanisms);
217b9d004c6Schristos 	if (major != GSS_S_COMPLETE)
218ca1c9b0cSelric 	    goto failure;
219ca1c9b0cSelric 
220ca1c9b0cSelric         HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
221ca1c9b0cSelric 
222b9d004c6Schristos         major = _gsskrb5_inquire_cred(minor_status, (gss_cred_id_t)cred,
223ca1c9b0cSelric                                       NULL, &lifetime, NULL, actual_mechs);
224b9d004c6Schristos         if (major != GSS_S_COMPLETE)
225ca1c9b0cSelric             goto failure;
226ca1c9b0cSelric 
227b9d004c6Schristos 	*output_cred_handle = (gss_cred_id_t)handle;
228b9d004c6Schristos     }
229b9d004c6Schristos 
230ca1c9b0cSelric     if (initiator_time_rec)
231ca1c9b0cSelric 	*initiator_time_rec = lifetime;
232ca1c9b0cSelric     if (acceptor_time_rec)
233ca1c9b0cSelric 	*acceptor_time_rec = lifetime;
234ca1c9b0cSelric 
235ca1c9b0cSelric     *minor_status = 0;
236b9d004c6Schristos     return major;
237ca1c9b0cSelric 
238ca1c9b0cSelric failure:
239ca1c9b0cSelric     if (handle) {
240ca1c9b0cSelric 	if (handle->principal)
241ca1c9b0cSelric 	    krb5_free_principal(context, handle->principal);
242ca1c9b0cSelric 	if (handle->keytab)
243ca1c9b0cSelric 	    krb5_kt_close(context, handle->keytab);
244ca1c9b0cSelric 	if (handle->ccache)
245ca1c9b0cSelric 	    krb5_cc_destroy(context, handle->ccache);
246ca1c9b0cSelric 	if (handle->mechanisms)
247ca1c9b0cSelric 	    gss_release_oid_set(NULL, &handle->mechanisms);
248ca1c9b0cSelric 	free(handle);
249ca1c9b0cSelric     }
250b9d004c6Schristos     if (cred && output_cred_handle)
251ca1c9b0cSelric 	HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
252b9d004c6Schristos     return major;
253ca1c9b0cSelric }
254