2 * Copyright (c) 2005 Doug Rabson
5 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
6 * Portions Copyright (c) 2011, 2018 PADL Software Pty Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/lib/libgssapi/gss_acquire_cred.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
32 #include "mech_locl.h"
35 * Shim for gss_acquire_cred_with_password()
38 find_password_in_cred_store(gss_const_key_value_set_t cred_store)
42 if (cred_store == GSS_C_NO_CRED_STORE)
45 for (i = 0; i < cred_store->count; i++) {
46 if (strcmp(cred_store->elements[i].key, "password") == 0)
47 return cred_store->elements[i].value;
54 acquire_mech_cred(OM_uint32 *minor_status,
55 gssapi_mech_interface m,
56 const struct _gss_mechanism_name *mn,
58 gss_cred_usage_t cred_usage,
59 gss_const_key_value_set_t cred_store,
60 struct _gss_mechanism_cred **out,
63 OM_uint32 major_status;
64 struct _gss_mechanism_cred *mc;
65 gss_OID_set_desc mech;
66 const char *spassword;
72 mc = calloc(1, sizeof(struct _gss_mechanism_cred));
74 *minor_status = ENOMEM;
79 mc->gmc_mech_oid = &m->gm_mech_oid;
82 mech.elements = mc->gmc_mech_oid;
84 if (m->gm_acquire_cred_from) {
85 major_status = m->gm_acquire_cred_from(minor_status,
86 mn ? mn->gmn_name : GSS_C_NO_NAME,
94 } else if (cred_store == GSS_C_NO_CRED_STORE && m->gm_acquire_cred) {
95 major_status = m->gm_acquire_cred(minor_status,
96 mn ? mn->gmn_name : GSS_C_NO_NAME,
103 } else if (m->gm_compat &&
104 m->gm_compat->gmc_acquire_cred_with_password &&
105 (spassword = find_password_in_cred_store(cred_store)) != NULL) {
106 gss_buffer_desc password;
108 password.length = strlen(spassword);
109 password.value = rk_UNCONST(spassword);
111 /* compat glue for loadable mechanisms that implement API-as-SPI */
112 major_status = m->gm_compat->gmc_acquire_cred_with_password(minor_status,
113 mn ? mn->gmn_name : GSS_C_NO_NAME,
122 major_status = GSS_S_UNAVAILABLE;
124 heim_assert(major_status == GSS_S_COMPLETE || mc->gmc_cred == NULL,
125 "gss_acquire_cred_from: mech succeeded but did not return a credential");
127 if (major_status == GSS_S_COMPLETE)
135 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
136 gss_acquire_cred_from(OM_uint32 *minor_status,
137 gss_const_name_t desired_name,
139 const gss_OID_set desired_mechs,
140 gss_cred_usage_t cred_usage,
141 gss_const_key_value_set_t cred_store,
142 gss_cred_id_t *output_cred_handle,
143 gss_OID_set *actual_mechs,
146 OM_uint32 major_status, minor;
147 struct _gss_name *name = (struct _gss_name *)desired_name;
148 gssapi_mech_interface m;
149 struct _gss_cred *cred = NULL;
151 OM_uint32 min_time = GSS_C_INDEFINITE;
152 gss_OID_set mechs = GSS_C_NO_OID_SET;
155 if (output_cred_handle == NULL)
156 return GSS_S_CALL_INACCESSIBLE_READ;
157 *output_cred_handle = GSS_C_NO_CREDENTIAL;
159 *actual_mechs = GSS_C_NO_OID_SET;
165 if (desired_mechs != GSS_C_NO_OID_SET) {
166 int only_mg_cred_mechs = -1;
168 for (i = 0; i < desired_mechs->count; i++) {
169 m = __gss_get_mechanism(&desired_mechs->elements[i]);
171 if ((m->gm_flags & GM_USE_MG_CRED) == 0)
172 only_mg_cred_mechs = 0;
173 else if (only_mg_cred_mechs == -1)
174 only_mg_cred_mechs = 1;
178 * Now SPNEGO supports GM_USE_MG_CRED it's no longer necessary
179 * to specifically acquire SPNEGO credentials. If the caller
180 * did not specify any concrete mechanisms then we will acquire
181 * credentials for all of them.
183 if (only_mg_cred_mechs == -1) {
185 major_status = GSS_S_BAD_MECH;
187 } else if (only_mg_cred_mechs == 0)
188 mechs = desired_mechs;
190 mechs = _gss_mech_oids;
192 mechs = _gss_mech_oids;
194 cred = _gss_mg_alloc_cred();
196 *minor_status = ENOMEM;
197 major_status = GSS_S_FAILURE;
202 major_status = gss_create_empty_oid_set(minor_status, actual_mechs);
203 if (GSS_ERROR(major_status))
207 major_status = GSS_S_UNAVAILABLE; /* in case of no mechs */
209 for (i = 0; i < mechs->count; i++) {
210 struct _gss_mechanism_name *mn = NULL;
211 struct _gss_mechanism_cred *mc = NULL;
214 m = __gss_get_mechanism(&mechs->elements[i]);
215 if (m == NULL || (m->gm_flags & GM_USE_MG_CRED) != 0)
218 if (desired_name != GSS_C_NO_NAME) {
219 major_status = _gss_find_mn(minor_status, name,
220 &mechs->elements[i], &mn);
221 if (major_status != GSS_S_COMPLETE)
225 major_status = acquire_mech_cred(minor_status, m, mn,
226 time_req, cred_usage,
227 cred_store, &mc, &cred_time);
228 if (major_status != GSS_S_COMPLETE) {
229 if (mechs->count == 1)
230 _gss_mg_error(m, *minor_status);
234 _gss_mg_log_name(10, name, &mechs->elements[i],
235 "gss_acquire_cred %s name: %ld/%ld",
237 (long)major_status, (long)*minor_status);
239 HEIM_TAILQ_INSERT_TAIL(&cred->gc_mc, mc, gmc_link);
241 if (cred_time < min_time)
242 min_time = cred_time;
243 if (actual_mechs != NULL) {
244 major_status = gss_add_oid_set_member(minor_status,
247 if (GSS_ERROR(major_status))
253 * If we didn't manage to create a single credential, return
256 if (!HEIM_TAILQ_FIRST(&cred->gc_mc)) {
257 if (mechs->count > 1) {
259 major_status = GSS_S_NO_CRED;
261 heim_assert(major_status != GSS_S_COMPLETE,
262 "lack of credentials must result in an error");
266 /* add all GM_USE_MG_CRED mechs such as SPNEGO */
267 if (actual_mechs != NULL) {
268 struct _gss_mech_switch *ms;
270 HEIM_TAILQ_FOREACH(ms, &_gss_mechs, gm_link) {
273 if ((m->gm_flags & GM_USE_MG_CRED) == 0)
276 major_status = gss_add_oid_set_member(minor_status,
279 if (GSS_ERROR(major_status))
285 major_status = GSS_S_COMPLETE;
287 *output_cred_handle = (gss_cred_id_t)cred;
289 *time_rec = min_time;
291 _gss_mg_log_cred(10, cred, "gss_acquire_cred_from");
294 if (major_status != GSS_S_COMPLETE) {
295 gss_release_cred(&minor, (gss_cred_id_t *)&cred);
297 gss_release_oid_set(&minor, actual_mechs);