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 
31 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
32 gss_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 = calloc(1, sizeof(struct _gss_cred));
69 	if (new_cred == NULL) {
70 		*minor_status = ENOMEM;
71 		return (GSS_S_FAILURE);
72 	}
73 	HEIM_SLIST_INIT(&new_cred->gc_mc);
74 
75 	/*
76 	 * Copy credentials from un-desired mechanisms to the new credential.
77 	 */
78 	if (cred) {
79 		HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
80 			struct _gss_mechanism_cred *copy_mc;
81 
82 			if (gss_oid_equal(mc->gmc_mech_oid, desired_mech)) {
83 				continue;
84 			}
85 			copy_mc = _gss_copy_cred(mc);
86 			if (copy_mc == NULL) {
87 				gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
88 				*minor_status = ENOMEM;
89 				return (GSS_S_FAILURE);
90 			}
91 			HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, copy_mc, gmc_link);
92 		}
93 	}
94 
95 	/*
96 	 * Figure out a suitable mn, if any.
97 	 */
98 	if (desired_name != GSS_C_NO_NAME) {
99 		major_status = _gss_find_mn(minor_status,
100 					    (struct _gss_name *) desired_name,
101 					    desired_mech,
102 					    &mn);
103 		if (major_status != GSS_S_COMPLETE) {
104 			gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
105 			return (major_status);
106 		}
107 	}
108 
109 	if (cred_usage == GSS_C_BOTH)
110 		time_req = initiator_time_req > acceptor_time_req ? acceptor_time_req : initiator_time_req;
111 	else if (cred_usage == GSS_C_INITIATE)
112 		time_req = initiator_time_req;
113 	else
114 		time_req = acceptor_time_req;
115 
116 	major_status = _gss_acquire_mech_cred(minor_status, m, mn,
117 					      GSS_C_CRED_PASSWORD, password,
118 					      time_req, desired_mech,
119 					      cred_usage, &mc);
120 	if (major_status != GSS_S_COMPLETE) {
121 		gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
122 		return (major_status);
123 	}
124 
125 	HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, mc, gmc_link);
126 
127 	if (actual_mechs || initiator_time_rec || acceptor_time_rec) {
128 		OM_uint32 time_rec;
129 
130 		major_status = gss_inquire_cred(minor_status,
131 						(gss_cred_id_t)new_cred,
132 						NULL,
133 						&time_rec,
134 						NULL,
135 						actual_mechs);
136 		if (GSS_ERROR(major_status)) {
137 			gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
138 			return (major_status);
139 		}
140 		if (initiator_time_rec &&
141 		    (cred_usage == GSS_C_INITIATE || cred_usage == GSS_C_BOTH))
142 			*initiator_time_rec = time_rec;
143 		if (acceptor_time_rec &&
144 		    (cred_usage == GSS_C_ACCEPT || cred_usage == GSS_C_BOTH))
145 			*acceptor_time_rec = time_rec;
146 	}
147 
148 	*output_cred_handle = (gss_cred_id_t) new_cred;
149 	return (GSS_S_COMPLETE);
150 }
151