1 /*	$NetBSD: gss_add_cred_with_password.c,v 1.1.1.1 2014/04/24 12:45:29 pettai Exp $	*/
2 
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  *	$FreeBSD: src/lib/libgssapi/gss_add_cred.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
29  */
30 
31 #include "mech_locl.h"
32 
33 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
gss_add_cred_with_password(OM_uint32 * minor_status,const gss_cred_id_t input_cred_handle,const gss_name_t desired_name,const gss_OID desired_mech,const gss_buffer_t password,gss_cred_usage_t cred_usage,OM_uint32 initiator_time_req,OM_uint32 acceptor_time_req,gss_cred_id_t * output_cred_handle,gss_OID_set * actual_mechs,OM_uint32 * initiator_time_rec,OM_uint32 * acceptor_time_rec)34 gss_add_cred_with_password(OM_uint32 *minor_status,
35     const gss_cred_id_t input_cred_handle,
36     const gss_name_t desired_name,
37     const gss_OID desired_mech,
38     const gss_buffer_t password,
39     gss_cred_usage_t cred_usage,
40     OM_uint32 initiator_time_req,
41     OM_uint32 acceptor_time_req,
42     gss_cred_id_t *output_cred_handle,
43     gss_OID_set *actual_mechs,
44     OM_uint32 *initiator_time_rec,
45     OM_uint32 *acceptor_time_rec)
46 {
47 	OM_uint32 major_status;
48 	gssapi_mech_interface m;
49 	struct _gss_cred *cred = (struct _gss_cred *) input_cred_handle;
50 	struct _gss_cred *new_cred;
51 	struct _gss_mechanism_cred *mc;
52 	struct _gss_mechanism_name *mn = NULL;
53 	OM_uint32 junk, time_req;
54 
55 	*minor_status = 0;
56 	*output_cred_handle = GSS_C_NO_CREDENTIAL;
57 	if (initiator_time_rec)
58 	    *initiator_time_rec = 0;
59 	if (acceptor_time_rec)
60 	    *acceptor_time_rec = 0;
61 	if (actual_mechs)
62 	    *actual_mechs = GSS_C_NO_OID_SET;
63 
64 	m = __gss_get_mechanism(desired_mech);
65 	if (m == NULL) {
66 		*minor_status = 0;
67 		return (GSS_S_BAD_MECH);
68 	}
69 
70 	new_cred = calloc(1, sizeof(struct _gss_cred));
71 	if (new_cred == NULL) {
72 		*minor_status = ENOMEM;
73 		return (GSS_S_FAILURE);
74 	}
75 	HEIM_SLIST_INIT(&new_cred->gc_mc);
76 
77 	/*
78 	 * Copy credentials from un-desired mechanisms to the new credential.
79 	 */
80 	if (cred) {
81 		HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
82 			struct _gss_mechanism_cred *copy_mc;
83 
84 			if (gss_oid_equal(mc->gmc_mech_oid, desired_mech)) {
85 				continue;
86 			}
87 			copy_mc = _gss_copy_cred(mc);
88 			if (copy_mc == NULL) {
89 				gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
90 				*minor_status = ENOMEM;
91 				return (GSS_S_FAILURE);
92 			}
93 			HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, copy_mc, gmc_link);
94 		}
95 	}
96 
97 	/*
98 	 * Figure out a suitable mn, if any.
99 	 */
100 	if (desired_name != GSS_C_NO_NAME) {
101 		major_status = _gss_find_mn(minor_status,
102 					    (struct _gss_name *) desired_name,
103 					    desired_mech,
104 					    &mn);
105 		if (major_status != GSS_S_COMPLETE) {
106 			gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
107 			return (major_status);
108 		}
109 	}
110 
111 	if (cred_usage == GSS_C_BOTH)
112 		time_req = initiator_time_req > acceptor_time_req ? acceptor_time_req : initiator_time_req;
113 	else if (cred_usage == GSS_C_INITIATE)
114 		time_req = initiator_time_req;
115 	else
116 		time_req = acceptor_time_req;
117 
118 	major_status = _gss_acquire_mech_cred(minor_status, m, mn,
119 					      GSS_C_CRED_PASSWORD, password,
120 					      time_req, desired_mech,
121 					      cred_usage, &mc);
122 	if (major_status != GSS_S_COMPLETE) {
123 		gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
124 		return (major_status);
125 	}
126 
127 	HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, mc, gmc_link);
128 
129 	if (actual_mechs || initiator_time_rec || acceptor_time_rec) {
130 		OM_uint32 time_rec;
131 
132 		major_status = gss_inquire_cred(minor_status,
133 						(gss_cred_id_t)new_cred,
134 						NULL,
135 						&time_rec,
136 						NULL,
137 						actual_mechs);
138 		if (GSS_ERROR(major_status)) {
139 			gss_release_cred(&junk, (gss_cred_id_t *)&new_cred);
140 			return (major_status);
141 		}
142 		if (initiator_time_rec &&
143 		    (cred_usage == GSS_C_INITIATE || cred_usage == GSS_C_BOTH))
144 			*initiator_time_rec = time_rec;
145 		if (acceptor_time_rec &&
146 		    (cred_usage == GSS_C_ACCEPT || cred_usage == GSS_C_BOTH))
147 			*acceptor_time_rec = time_rec;
148 	}
149 
150 	*output_cred_handle = (gss_cred_id_t) new_cred;
151 	return (GSS_S_COMPLETE);
152 }
153