1/*-
2 * Copyright (c) 2005 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *	$FreeBSD: src/lib/libgssapi/gss_add_cred.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
27 */
28
29#include "mech_locl.h"
30
31GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
32gss_add_cred_with_password(OM_uint32 *minor_status,
33    const gss_cred_id_t input_cred_handle,
34    const gss_name_t desired_name,
35    const gss_OID desired_mech,
36    const gss_buffer_t password,
37    gss_cred_usage_t cred_usage,
38    OM_uint32 initiator_time_req,
39    OM_uint32 acceptor_time_req,
40    gss_cred_id_t *output_cred_handle,
41    gss_OID_set *actual_mechs,
42    OM_uint32 *initiator_time_rec,
43    OM_uint32 *acceptor_time_rec)
44{
45	OM_uint32 major_status;
46	gssapi_mech_interface m;
47	struct _gss_cred *cred = (struct _gss_cred *) input_cred_handle;
48	struct _gss_cred *new_cred;
49	struct _gss_mechanism_cred *mc;
50	struct _gss_mechanism_name *mn = NULL;
51	OM_uint32 junk, time_req;
52
53	*minor_status = 0;
54	*output_cred_handle = GSS_C_NO_CREDENTIAL;
55	if (initiator_time_rec)
56	    *initiator_time_rec = 0;
57	if (acceptor_time_rec)
58	    *acceptor_time_rec = 0;
59	if (actual_mechs)
60	    *actual_mechs = GSS_C_NO_OID_SET;
61
62	m = __gss_get_mechanism(desired_mech);
63	if (m == NULL) {
64		*minor_status = 0;
65		return (GSS_S_BAD_MECH);
66	}
67
68	new_cred = _gss_mg_alloc_cred();
69	if (new_cred == NULL) {
70		*minor_status = ENOMEM;
71		return (GSS_S_FAILURE);
72	}
73
74	/*
75	 * Copy credentials from un-desired mechanisms to the new credential.
76	 */
77	if (cred) {
78		HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
79			struct _gss_mechanism_cred *copy_mc;
80
81			if (gss_oid_equal(mc->gmc_mech_oid, desired_mech)) {
82				continue;
83			}
84			copy_mc = _gss_copy_cred(mc);
85			if (copy_mc == NULL) {
86				gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
87				*minor_status = ENOMEM;
88				return (GSS_S_FAILURE);
89			}
90			HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, copy_mc, gmc_link);
91		}
92	}
93
94	/*
95	 * Figure out a suitable mn, if any.
96	 */
97	if (desired_name != GSS_C_NO_NAME) {
98		major_status = _gss_find_mn(minor_status,
99					    (struct _gss_name *) desired_name,
100					    desired_mech,
101					    &mn);
102		if (major_status != GSS_S_COMPLETE) {
103			gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
104			return (major_status);
105		}
106	}
107
108	if (cred_usage == GSS_C_BOTH)
109		time_req = initiator_time_req > acceptor_time_req ? acceptor_time_req : initiator_time_req;
110	else if (cred_usage == GSS_C_INITIATE)
111		time_req = initiator_time_req;
112	else
113		time_req = acceptor_time_req;
114
115	major_status = _gss_acquire_mech_cred(minor_status, m, mn,
116					      GSS_C_CRED_PASSWORD, password,
117					      time_req, desired_mech,
118					      cred_usage, &mc);
119	if (major_status != GSS_S_COMPLETE) {
120		gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
121		return (major_status);
122	}
123
124	HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, mc, gmc_link);
125
126	if (actual_mechs || initiator_time_rec || acceptor_time_rec) {
127		OM_uint32 time_rec;
128
129		major_status = gss_inquire_cred(minor_status,
130						(gss_cred_id_t)new_cred,
131						NULL,
132						&time_rec,
133						NULL,
134						actual_mechs);
135		if (GSS_ERROR(major_status)) {
136			gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
137			return (major_status);
138		}
139		if (initiator_time_rec &&
140		    (cred_usage == GSS_C_INITIATE || cred_usage == GSS_C_BOTH))
141			*initiator_time_rec = time_rec;
142		if (acceptor_time_rec &&
143		    (cred_usage == GSS_C_ACCEPT || cred_usage == GSS_C_BOTH))
144			*acceptor_time_rec = time_rec;
145	}
146
147	*output_cred_handle = (gss_cred_id_t) new_cred;
148	return (GSS_S_COMPLETE);
149}
150