1 /*	$NetBSD: cred_stubs.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 2004, PADL Software Pty Ltd.
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of PADL Software nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include "spnego_locl.h"
36 
37 OM_uint32 GSSAPI_CALLCONV
_gss_spnego_release_cred(OM_uint32 * minor_status,gss_cred_id_t * cred_handle)38 _gss_spnego_release_cred(OM_uint32 *minor_status, gss_cred_id_t *cred_handle)
39 {
40     OM_uint32 ret;
41 
42     *minor_status = 0;
43 
44     if (cred_handle == NULL || *cred_handle == GSS_C_NO_CREDENTIAL)
45 	return GSS_S_COMPLETE;
46 
47     ret = gss_release_cred(minor_status, cred_handle);
48 
49     *cred_handle = GSS_C_NO_CREDENTIAL;
50 
51     return ret;
52 }
53 
54 /*
55  * For now, just a simple wrapper that avoids recursion. When
56  * we support gss_{get,set}_neg_mechs() we will need to expose
57  * more functionality.
58  */
_gss_spnego_acquire_cred(OM_uint32 * minor_status,const gss_name_t desired_name,OM_uint32 time_req,const gss_OID_set desired_mechs,gss_cred_usage_t cred_usage,gss_cred_id_t * output_cred_handle,gss_OID_set * actual_mechs,OM_uint32 * time_rec)59 OM_uint32 GSSAPI_CALLCONV _gss_spnego_acquire_cred
60 (OM_uint32 *minor_status,
61  const gss_name_t desired_name,
62  OM_uint32 time_req,
63  const gss_OID_set desired_mechs,
64  gss_cred_usage_t cred_usage,
65  gss_cred_id_t * output_cred_handle,
66  gss_OID_set * actual_mechs,
67  OM_uint32 * time_rec
68     )
69 {
70     const spnego_name dname = (const spnego_name)desired_name;
71     gss_name_t name = GSS_C_NO_NAME;
72     OM_uint32 ret, tmp;
73     gss_OID_set_desc actual_desired_mechs;
74     gss_OID_set mechs;
75     size_t i, j;
76 
77     *output_cred_handle = GSS_C_NO_CREDENTIAL;
78 
79     if (dname) {
80 	ret = gss_import_name(minor_status, &dname->value, &dname->type, &name);
81 	if (ret) {
82 	    return ret;
83 	}
84     }
85 
86     ret = gss_indicate_mechs(minor_status, &mechs);
87     if (ret != GSS_S_COMPLETE) {
88 	gss_release_name(minor_status, &name);
89 	return ret;
90     }
91 
92     /* Remove ourselves from this list */
93     actual_desired_mechs.count = mechs->count;
94     actual_desired_mechs.elements = malloc(actual_desired_mechs.count *
95 					   sizeof(gss_OID_desc));
96     if (actual_desired_mechs.elements == NULL) {
97 	*minor_status = ENOMEM;
98 	ret = GSS_S_FAILURE;
99 	goto out;
100     }
101 
102     for (i = 0, j = 0; i < mechs->count; i++) {
103 	if (gss_oid_equal(&mechs->elements[i], GSS_SPNEGO_MECHANISM))
104 	    continue;
105 
106 	actual_desired_mechs.elements[j] = mechs->elements[i];
107 	j++;
108     }
109     actual_desired_mechs.count = j;
110 
111     ret = gss_acquire_cred(minor_status, name,
112 			   time_req, &actual_desired_mechs,
113 			   cred_usage,
114 			   output_cred_handle,
115 			   actual_mechs, time_rec);
116     if (ret != GSS_S_COMPLETE)
117 	goto out;
118 
119 out:
120     gss_release_name(minor_status, &name);
121     gss_release_oid_set(&tmp, &mechs);
122     if (actual_desired_mechs.elements != NULL) {
123 	free(actual_desired_mechs.elements);
124     }
125     if (ret != GSS_S_COMPLETE) {
126 	_gss_spnego_release_cred(&tmp, output_cred_handle);
127     }
128 
129     return ret;
130 }
131 
_gss_spnego_inquire_cred(OM_uint32 * minor_status,const gss_cred_id_t cred_handle,gss_name_t * name,OM_uint32 * lifetime,gss_cred_usage_t * cred_usage,gss_OID_set * mechanisms)132 OM_uint32 GSSAPI_CALLCONV _gss_spnego_inquire_cred
133            (OM_uint32 * minor_status,
134             const gss_cred_id_t cred_handle,
135             gss_name_t * name,
136             OM_uint32 * lifetime,
137             gss_cred_usage_t * cred_usage,
138             gss_OID_set * mechanisms
139            )
140 {
141     spnego_name sname = NULL;
142     OM_uint32 ret;
143 
144     if (cred_handle == GSS_C_NO_CREDENTIAL) {
145 	*minor_status = 0;
146 	return GSS_S_NO_CRED;
147     }
148 
149     if (name) {
150 	sname = calloc(1, sizeof(*sname));
151 	if (sname == NULL) {
152 	    *minor_status = ENOMEM;
153 	    return GSS_S_FAILURE;
154 	}
155     }
156 
157     ret = gss_inquire_cred(minor_status,
158 			   cred_handle,
159 			   sname ? &sname->mech : NULL,
160 			   lifetime,
161 			   cred_usage,
162 			   mechanisms);
163     if (ret) {
164 	if (sname)
165 	    free(sname);
166 	return ret;
167     }
168     if (name)
169 	*name = (gss_name_t)sname;
170 
171     return ret;
172 }
173 
_gss_spnego_inquire_cred_by_mech(OM_uint32 * minor_status,const gss_cred_id_t cred_handle,const gss_OID mech_type,gss_name_t * name,OM_uint32 * initiator_lifetime,OM_uint32 * acceptor_lifetime,gss_cred_usage_t * cred_usage)174 OM_uint32 GSSAPI_CALLCONV _gss_spnego_inquire_cred_by_mech (
175             OM_uint32 * minor_status,
176             const gss_cred_id_t cred_handle,
177             const gss_OID mech_type,
178             gss_name_t * name,
179             OM_uint32 * initiator_lifetime,
180             OM_uint32 * acceptor_lifetime,
181             gss_cred_usage_t * cred_usage
182            )
183 {
184     spnego_name sname = NULL;
185     OM_uint32 ret;
186 
187     if (cred_handle == GSS_C_NO_CREDENTIAL) {
188 	*minor_status = 0;
189 	return GSS_S_NO_CRED;
190     }
191 
192     if (name) {
193 	sname = calloc(1, sizeof(*sname));
194 	if (sname == NULL) {
195 	    *minor_status = ENOMEM;
196 	    return GSS_S_FAILURE;
197 	}
198     }
199 
200     ret = gss_inquire_cred_by_mech(minor_status,
201 				   cred_handle,
202 				   mech_type,
203 				   sname ? &sname->mech : NULL,
204 				   initiator_lifetime,
205 				   acceptor_lifetime,
206 				   cred_usage);
207 
208     if (ret) {
209 	if (sname)
210 	    free(sname);
211 	return ret;
212     }
213     if (name)
214 	*name = (gss_name_t)sname;
215 
216     return GSS_S_COMPLETE;
217 }
218 
_gss_spnego_inquire_cred_by_oid(OM_uint32 * minor_status,const gss_cred_id_t cred_handle,const gss_OID desired_object,gss_buffer_set_t * data_set)219 OM_uint32 GSSAPI_CALLCONV _gss_spnego_inquire_cred_by_oid
220            (OM_uint32 * minor_status,
221             const gss_cred_id_t cred_handle,
222             const gss_OID desired_object,
223             gss_buffer_set_t *data_set)
224 {
225     OM_uint32 ret;
226 
227     if (cred_handle == GSS_C_NO_CREDENTIAL) {
228 	*minor_status = 0;
229 	return GSS_S_NO_CRED;
230     }
231 
232     ret = gss_inquire_cred_by_oid(minor_status,
233 				  cred_handle,
234 				  desired_object,
235 				  data_set);
236 
237     return ret;
238 }
239 
240 OM_uint32 GSSAPI_CALLCONV
_gss_spnego_set_cred_option(OM_uint32 * minor_status,gss_cred_id_t * cred_handle,const gss_OID object,const gss_buffer_t value)241 _gss_spnego_set_cred_option (OM_uint32 *minor_status,
242 			     gss_cred_id_t *cred_handle,
243 			     const gss_OID object,
244 			     const gss_buffer_t value)
245 {
246     if (cred_handle == NULL || *cred_handle == GSS_C_NO_CREDENTIAL) {
247 	*minor_status = 0;
248 	return GSS_S_NO_CRED;
249     }
250 
251     return gss_set_cred_option(minor_status,
252 			      cred_handle,
253 			      object,
254 			      value);
255 }
256 
257 
258 OM_uint32 GSSAPI_CALLCONV
_gss_spnego_export_cred(OM_uint32 * minor_status,gss_cred_id_t cred_handle,gss_buffer_t value)259 _gss_spnego_export_cred (OM_uint32 *minor_status,
260 			 gss_cred_id_t cred_handle,
261 			 gss_buffer_t value)
262 {
263     return gss_export_cred(minor_status, cred_handle, value);
264 }
265 
266 OM_uint32 GSSAPI_CALLCONV
_gss_spnego_import_cred(OM_uint32 * minor_status,gss_buffer_t value,gss_cred_id_t * cred_handle)267 _gss_spnego_import_cred (OM_uint32 *minor_status,
268 			 gss_buffer_t value,
269 			 gss_cred_id_t *cred_handle)
270 {
271     return gss_import_cred(minor_status, value, cred_handle);
272 }
273 
274