xref: /freebsd/lib/libgssapi/gss_add_cred.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <gssapi/gssapi.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 
33 #include "mech_switch.h"
34 #include "cred.h"
35 #include "name.h"
36 #include "utils.h"
37 
38 static struct _gss_mechanism_cred *
39 _gss_copy_cred(struct _gss_mechanism_cred *mc)
40 {
41 	struct _gss_mechanism_cred *new_mc;
42 	struct _gss_mech_switch *m = mc->gmc_mech;
43 	OM_uint32 major_status, minor_status;
44 	gss_name_t name;
45 	gss_cred_id_t cred;
46 	OM_uint32 initiator_lifetime, acceptor_lifetime;
47 	gss_cred_usage_t cred_usage;
48 
49 	major_status = m->gm_inquire_cred_by_mech(&minor_status,
50 	    mc->gmc_cred, mc->gmc_mech_oid,
51 	    &name, &initiator_lifetime, &acceptor_lifetime, &cred_usage);
52 	if (major_status) {
53 		_gss_mg_error(m, major_status, minor_status);
54 		return (0);
55 	}
56 
57 	major_status = m->gm_add_cred(&minor_status,
58 	    GSS_C_NO_CREDENTIAL, name, mc->gmc_mech_oid,
59 	    cred_usage, initiator_lifetime, acceptor_lifetime,
60 	    &cred, 0, 0, 0);
61 	m->gm_release_name(&minor_status, &name);
62 
63 	if (major_status) {
64 		_gss_mg_error(m, major_status, minor_status);
65 		return (0);
66 	}
67 
68 	new_mc = malloc(sizeof(struct _gss_mechanism_cred));
69 	if (!new_mc) {
70 		m->gm_release_cred(&minor_status, &cred);
71 		return (0);
72 	}
73 	new_mc->gmc_mech = m;
74 	new_mc->gmc_mech_oid = &m->gm_mech_oid;
75 	new_mc->gmc_cred = cred;
76 
77 	return (new_mc);
78 }
79 
80 OM_uint32
81 gss_add_cred(OM_uint32 *minor_status,
82     const gss_cred_id_t input_cred_handle,
83     const gss_name_t desired_name,
84     const gss_OID desired_mech,
85     gss_cred_usage_t cred_usage,
86     OM_uint32 initiator_time_req,
87     OM_uint32 acceptor_time_req,
88     gss_cred_id_t *output_cred_handle,
89     gss_OID_set *actual_mechs,
90     OM_uint32 *initiator_time_rec,
91     OM_uint32 *acceptor_time_rec)
92 {
93 	OM_uint32 major_status;
94 	struct _gss_mech_switch *m;
95 	struct _gss_cred *cred = (struct _gss_cred *) input_cred_handle;
96 	struct _gss_cred *new_cred;
97 	gss_cred_id_t release_cred;
98 	struct _gss_mechanism_cred *mc, *target_mc, *copy_mc;
99 	struct _gss_mechanism_name *mn;
100 	OM_uint32 junk;
101 
102 	*minor_status = 0;
103 	*output_cred_handle = GSS_C_NO_CREDENTIAL;
104 	if (initiator_time_rec)
105 		*initiator_time_rec = 0;
106 	if (acceptor_time_rec)
107 		*acceptor_time_rec = 0;
108 	if (actual_mechs)
109 		*actual_mechs = GSS_C_NO_OID_SET;
110 
111 	new_cred = malloc(sizeof(struct _gss_cred));
112 	if (!new_cred) {
113 		*minor_status = ENOMEM;
114 		return (GSS_S_FAILURE);
115 	}
116 	SLIST_INIT(&new_cred->gc_mc);
117 
118 	/*
119 	 * We go through all the mc attached to the input_cred_handle
120 	 * and check the mechanism. If it matches, we call
121 	 * gss_add_cred for that mechanism, otherwise we copy the mc
122 	 * to new_cred.
123 	 */
124 	target_mc = NULL;
125 	if (cred) {
126 		SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
127 			if (gss_oid_equal(mc->gmc_mech_oid, desired_mech)) {
128 				target_mc = mc;
129 			}
130 			copy_mc = _gss_copy_cred(mc);
131 			if (!copy_mc) {
132 				release_cred = (gss_cred_id_t) new_cred;
133 				gss_release_cred(&junk, &release_cred);
134 				*minor_status = ENOMEM;
135 				return (GSS_S_FAILURE);
136 			}
137 			SLIST_INSERT_HEAD(&new_cred->gc_mc, copy_mc, gmc_link);
138 		}
139 	}
140 
141 	/*
142 	 * Figure out a suitable mn, if any.
143 	 */
144 	if (desired_name) {
145 		major_status = _gss_find_mn(minor_status,
146 					    (struct _gss_name *) desired_name,
147 					    desired_mech,
148 					    &mn);
149 		if (major_status != GSS_S_COMPLETE) {
150 			free(new_cred);
151 			return (major_status);
152 		}
153 	} else {
154 		mn = NULL;
155 	}
156 
157 	m = _gss_find_mech_switch(desired_mech);
158 
159 	mc = malloc(sizeof(struct _gss_mechanism_cred));
160 	if (!mc) {
161 		release_cred = (gss_cred_id_t) new_cred;
162 		gss_release_cred(&junk, &release_cred);
163 		*minor_status = ENOMEM;
164 		return (GSS_S_FAILURE);
165 	}
166 	mc->gmc_mech = m;
167 	mc->gmc_mech_oid = &m->gm_mech_oid;
168 
169 	major_status = m->gm_add_cred(minor_status,
170 	    target_mc ? target_mc->gmc_cred : GSS_C_NO_CREDENTIAL,
171 	    desired_name ? mn->gmn_name : GSS_C_NO_NAME,
172 	    desired_mech,
173 	    cred_usage,
174 	    initiator_time_req,
175 	    acceptor_time_req,
176 	    &mc->gmc_cred,
177 	    actual_mechs,
178 	    initiator_time_rec,
179 	    acceptor_time_rec);
180 
181 	if (major_status) {
182 		_gss_mg_error(m, major_status, *minor_status);
183 		release_cred = (gss_cred_id_t) new_cred;
184 		gss_release_cred(&junk, &release_cred);
185 		free(mc);
186 		return (major_status);
187 	}
188 	SLIST_INSERT_HEAD(&new_cred->gc_mc, mc, gmc_link);
189 	*output_cred_handle = (gss_cred_id_t) new_cred;
190 
191 	return (GSS_S_COMPLETE);
192 }
193 
194