xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/creds.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: creds.c,v 1.1.1.2 2014/04/24 12:45:49 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "krb5_locl.h"
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc /**
39ebfedea0SLionel Sambuc  * Free content of krb5_creds.
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * @param context Kerberos 5 context.
42ebfedea0SLionel Sambuc  * @param c krb5_creds to free.
43ebfedea0SLionel Sambuc  *
44ebfedea0SLionel Sambuc  * @return Returns 0 to indicate success. Otherwise an kerberos et
45ebfedea0SLionel Sambuc  * error code is returned, see krb5_get_error_message().
46ebfedea0SLionel Sambuc  *
47ebfedea0SLionel Sambuc  * @ingroup krb5
48ebfedea0SLionel Sambuc  */
49ebfedea0SLionel Sambuc 
50ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_free_cred_contents(krb5_context context,krb5_creds * c)51ebfedea0SLionel Sambuc krb5_free_cred_contents (krb5_context context, krb5_creds *c)
52ebfedea0SLionel Sambuc {
53ebfedea0SLionel Sambuc     krb5_free_principal (context, c->client);
54ebfedea0SLionel Sambuc     c->client = NULL;
55ebfedea0SLionel Sambuc     krb5_free_principal (context, c->server);
56ebfedea0SLionel Sambuc     c->server = NULL;
57ebfedea0SLionel Sambuc     krb5_free_keyblock_contents (context, &c->session);
58ebfedea0SLionel Sambuc     krb5_data_free (&c->ticket);
59ebfedea0SLionel Sambuc     krb5_data_free (&c->second_ticket);
60ebfedea0SLionel Sambuc     free_AuthorizationData (&c->authdata);
61ebfedea0SLionel Sambuc     krb5_free_addresses (context, &c->addresses);
62ebfedea0SLionel Sambuc     memset(c, 0, sizeof(*c));
63ebfedea0SLionel Sambuc     return 0;
64ebfedea0SLionel Sambuc }
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc /**
67ebfedea0SLionel Sambuc  * Copy content of krb5_creds.
68ebfedea0SLionel Sambuc  *
69ebfedea0SLionel Sambuc  * @param context Kerberos 5 context.
70ebfedea0SLionel Sambuc  * @param incred source credential
71ebfedea0SLionel Sambuc  * @param c destination credential, free with krb5_free_cred_contents().
72ebfedea0SLionel Sambuc  *
73ebfedea0SLionel Sambuc  * @return Returns 0 to indicate success. Otherwise an kerberos et
74ebfedea0SLionel Sambuc  * error code is returned, see krb5_get_error_message().
75ebfedea0SLionel Sambuc  *
76ebfedea0SLionel Sambuc  * @ingroup krb5
77ebfedea0SLionel Sambuc  */
78ebfedea0SLionel Sambuc 
79ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_copy_creds_contents(krb5_context context,const krb5_creds * incred,krb5_creds * c)80ebfedea0SLionel Sambuc krb5_copy_creds_contents (krb5_context context,
81ebfedea0SLionel Sambuc 			  const krb5_creds *incred,
82ebfedea0SLionel Sambuc 			  krb5_creds *c)
83ebfedea0SLionel Sambuc {
84ebfedea0SLionel Sambuc     krb5_error_code ret;
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc     memset(c, 0, sizeof(*c));
87ebfedea0SLionel Sambuc     ret = krb5_copy_principal (context, incred->client, &c->client);
88ebfedea0SLionel Sambuc     if (ret)
89ebfedea0SLionel Sambuc 	goto fail;
90ebfedea0SLionel Sambuc     ret = krb5_copy_principal (context, incred->server, &c->server);
91ebfedea0SLionel Sambuc     if (ret)
92ebfedea0SLionel Sambuc 	goto fail;
93ebfedea0SLionel Sambuc     ret = krb5_copy_keyblock_contents (context, &incred->session, &c->session);
94ebfedea0SLionel Sambuc     if (ret)
95ebfedea0SLionel Sambuc 	goto fail;
96ebfedea0SLionel Sambuc     c->times = incred->times;
97ebfedea0SLionel Sambuc     ret = krb5_data_copy (&c->ticket,
98ebfedea0SLionel Sambuc 			  incred->ticket.data,
99ebfedea0SLionel Sambuc 			  incred->ticket.length);
100ebfedea0SLionel Sambuc     if (ret)
101ebfedea0SLionel Sambuc 	goto fail;
102ebfedea0SLionel Sambuc     ret = krb5_data_copy (&c->second_ticket,
103ebfedea0SLionel Sambuc 			  incred->second_ticket.data,
104ebfedea0SLionel Sambuc 			  incred->second_ticket.length);
105ebfedea0SLionel Sambuc     if (ret)
106ebfedea0SLionel Sambuc 	goto fail;
107ebfedea0SLionel Sambuc     ret = copy_AuthorizationData(&incred->authdata, &c->authdata);
108ebfedea0SLionel Sambuc     if (ret)
109ebfedea0SLionel Sambuc 	goto fail;
110ebfedea0SLionel Sambuc     ret = krb5_copy_addresses (context,
111ebfedea0SLionel Sambuc 			       &incred->addresses,
112ebfedea0SLionel Sambuc 			       &c->addresses);
113ebfedea0SLionel Sambuc     if (ret)
114ebfedea0SLionel Sambuc 	goto fail;
115ebfedea0SLionel Sambuc     c->flags = incred->flags;
116ebfedea0SLionel Sambuc     return 0;
117ebfedea0SLionel Sambuc 
118ebfedea0SLionel Sambuc fail:
119ebfedea0SLionel Sambuc     krb5_free_cred_contents (context, c);
120ebfedea0SLionel Sambuc     return ret;
121ebfedea0SLionel Sambuc }
122ebfedea0SLionel Sambuc 
123ebfedea0SLionel Sambuc /**
124ebfedea0SLionel Sambuc  * Copy krb5_creds.
125ebfedea0SLionel Sambuc  *
126ebfedea0SLionel Sambuc  * @param context Kerberos 5 context.
127ebfedea0SLionel Sambuc  * @param incred source credential
128ebfedea0SLionel Sambuc  * @param outcred destination credential, free with krb5_free_creds().
129ebfedea0SLionel Sambuc  *
130ebfedea0SLionel Sambuc  * @return Returns 0 to indicate success. Otherwise an kerberos et
131ebfedea0SLionel Sambuc  * error code is returned, see krb5_get_error_message().
132ebfedea0SLionel Sambuc  *
133ebfedea0SLionel Sambuc  * @ingroup krb5
134ebfedea0SLionel Sambuc  */
135ebfedea0SLionel Sambuc 
136ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_copy_creds(krb5_context context,const krb5_creds * incred,krb5_creds ** outcred)137ebfedea0SLionel Sambuc krb5_copy_creds (krb5_context context,
138ebfedea0SLionel Sambuc 		 const krb5_creds *incred,
139ebfedea0SLionel Sambuc 		 krb5_creds **outcred)
140ebfedea0SLionel Sambuc {
141ebfedea0SLionel Sambuc     krb5_creds *c;
142ebfedea0SLionel Sambuc 
143ebfedea0SLionel Sambuc     c = malloc (sizeof (*c));
144ebfedea0SLionel Sambuc     if (c == NULL) {
145ebfedea0SLionel Sambuc 	krb5_set_error_message (context, ENOMEM,
146ebfedea0SLionel Sambuc 				N_("malloc: out of memory", ""));
147ebfedea0SLionel Sambuc 	return ENOMEM;
148ebfedea0SLionel Sambuc     }
149ebfedea0SLionel Sambuc     memset (c, 0, sizeof(*c));
150ebfedea0SLionel Sambuc     *outcred = c;
151ebfedea0SLionel Sambuc     return krb5_copy_creds_contents (context, incred, c);
152ebfedea0SLionel Sambuc }
153ebfedea0SLionel Sambuc 
154ebfedea0SLionel Sambuc /**
155ebfedea0SLionel Sambuc  * Free krb5_creds.
156ebfedea0SLionel Sambuc  *
157ebfedea0SLionel Sambuc  * @param context Kerberos 5 context.
158ebfedea0SLionel Sambuc  * @param c krb5_creds to free.
159ebfedea0SLionel Sambuc  *
160ebfedea0SLionel Sambuc  * @return Returns 0 to indicate success. Otherwise an kerberos et
161ebfedea0SLionel Sambuc  * error code is returned, see krb5_get_error_message().
162ebfedea0SLionel Sambuc  *
163ebfedea0SLionel Sambuc  * @ingroup krb5
164ebfedea0SLionel Sambuc  */
165ebfedea0SLionel Sambuc 
166ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_free_creds(krb5_context context,krb5_creds * c)167ebfedea0SLionel Sambuc krb5_free_creds (krb5_context context, krb5_creds *c)
168ebfedea0SLionel Sambuc {
169ebfedea0SLionel Sambuc     krb5_free_cred_contents (context, c);
170ebfedea0SLionel Sambuc     free (c);
171ebfedea0SLionel Sambuc     return 0;
172ebfedea0SLionel Sambuc }
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc /* XXX this do not belong here */
175ebfedea0SLionel Sambuc static krb5_boolean
krb5_times_equal(const krb5_times * a,const krb5_times * b)176ebfedea0SLionel Sambuc krb5_times_equal(const krb5_times *a, const krb5_times *b)
177ebfedea0SLionel Sambuc {
178ebfedea0SLionel Sambuc     return a->starttime == b->starttime &&
179ebfedea0SLionel Sambuc 	a->authtime == b->authtime &&
180ebfedea0SLionel Sambuc 	a->endtime == b->endtime &&
181ebfedea0SLionel Sambuc 	a->renew_till == b->renew_till;
182ebfedea0SLionel Sambuc }
183ebfedea0SLionel Sambuc 
184ebfedea0SLionel Sambuc /**
185ebfedea0SLionel Sambuc  * Return TRUE if `mcreds' and `creds' are equal (`whichfields'
186ebfedea0SLionel Sambuc  * determines what equal means).
187ebfedea0SLionel Sambuc  *
188ebfedea0SLionel Sambuc  *
189ebfedea0SLionel Sambuc  * The following flags, set in whichfields affects the comparison:
190ebfedea0SLionel Sambuc  * - KRB5_TC_MATCH_SRV_NAMEONLY Consider all realms equal when comparing the service principal.
191ebfedea0SLionel Sambuc  * - KRB5_TC_MATCH_KEYTYPE Compare enctypes.
192ebfedea0SLionel Sambuc  * - KRB5_TC_MATCH_FLAGS_EXACT Make sure that the ticket flags are identical.
193ebfedea0SLionel Sambuc  * - KRB5_TC_MATCH_FLAGS Make sure that all ticket flags set in mcreds are also present in creds .
194ebfedea0SLionel Sambuc  * - KRB5_TC_MATCH_TIMES_EXACT Compares the ticket times exactly.
195ebfedea0SLionel Sambuc  * - KRB5_TC_MATCH_TIMES Compares only the expiration times of the creds.
196ebfedea0SLionel Sambuc  * - KRB5_TC_MATCH_AUTHDATA Compares the authdata fields.
197ebfedea0SLionel Sambuc  * - KRB5_TC_MATCH_2ND_TKT Compares the second tickets (used by user-to-user authentication).
198ebfedea0SLionel Sambuc  * - KRB5_TC_MATCH_IS_SKEY Compares the existance of the second ticket.
199ebfedea0SLionel Sambuc  *
200ebfedea0SLionel Sambuc  * @param context Kerberos 5 context.
201ebfedea0SLionel Sambuc  * @param whichfields which fields to compare.
202ebfedea0SLionel Sambuc  * @param mcreds cred to compare with.
203ebfedea0SLionel Sambuc  * @param creds cred to compare with.
204ebfedea0SLionel Sambuc  *
205ebfedea0SLionel Sambuc  * @return return TRUE if mcred and creds are equal, FALSE if not.
206ebfedea0SLionel Sambuc  *
207ebfedea0SLionel Sambuc  * @ingroup krb5
208ebfedea0SLionel Sambuc  */
209ebfedea0SLionel Sambuc 
210ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
krb5_compare_creds(krb5_context context,krb5_flags whichfields,const krb5_creds * mcreds,const krb5_creds * creds)211ebfedea0SLionel Sambuc krb5_compare_creds(krb5_context context, krb5_flags whichfields,
212ebfedea0SLionel Sambuc 		   const krb5_creds * mcreds, const krb5_creds * creds)
213ebfedea0SLionel Sambuc {
214ebfedea0SLionel Sambuc     krb5_boolean match = TRUE;
215ebfedea0SLionel Sambuc 
216ebfedea0SLionel Sambuc     if (match && mcreds->server) {
217ebfedea0SLionel Sambuc 	if (whichfields & (KRB5_TC_DONT_MATCH_REALM | KRB5_TC_MATCH_SRV_NAMEONLY))
218ebfedea0SLionel Sambuc 	    match = krb5_principal_compare_any_realm (context, mcreds->server,
219ebfedea0SLionel Sambuc 						      creds->server);
220ebfedea0SLionel Sambuc 	else
221ebfedea0SLionel Sambuc 	    match = krb5_principal_compare (context, mcreds->server,
222ebfedea0SLionel Sambuc 					    creds->server);
223ebfedea0SLionel Sambuc     }
224ebfedea0SLionel Sambuc 
225ebfedea0SLionel Sambuc     if (match && mcreds->client) {
226ebfedea0SLionel Sambuc 	if(whichfields & KRB5_TC_DONT_MATCH_REALM)
227ebfedea0SLionel Sambuc 	    match = krb5_principal_compare_any_realm (context, mcreds->client,
228ebfedea0SLionel Sambuc 						      creds->client);
229ebfedea0SLionel Sambuc 	else
230ebfedea0SLionel Sambuc 	    match = krb5_principal_compare (context, mcreds->client,
231ebfedea0SLionel Sambuc 					    creds->client);
232ebfedea0SLionel Sambuc     }
233ebfedea0SLionel Sambuc 
234ebfedea0SLionel Sambuc     if (match && (whichfields & KRB5_TC_MATCH_KEYTYPE))
235ebfedea0SLionel Sambuc         match = mcreds->session.keytype == creds->session.keytype;
236ebfedea0SLionel Sambuc 
237ebfedea0SLionel Sambuc     if (match && (whichfields & KRB5_TC_MATCH_FLAGS_EXACT))
238ebfedea0SLionel Sambuc 	match = mcreds->flags.i == creds->flags.i;
239ebfedea0SLionel Sambuc 
240ebfedea0SLionel Sambuc     if (match && (whichfields & KRB5_TC_MATCH_FLAGS))
241ebfedea0SLionel Sambuc 	match = (creds->flags.i & mcreds->flags.i) == mcreds->flags.i;
242ebfedea0SLionel Sambuc 
243ebfedea0SLionel Sambuc     if (match && (whichfields & KRB5_TC_MATCH_TIMES_EXACT))
244ebfedea0SLionel Sambuc 	match = krb5_times_equal(&mcreds->times, &creds->times);
245ebfedea0SLionel Sambuc 
246ebfedea0SLionel Sambuc     if (match && (whichfields & KRB5_TC_MATCH_TIMES))
247ebfedea0SLionel Sambuc 	/* compare only expiration times */
248ebfedea0SLionel Sambuc 	match = (mcreds->times.renew_till <= creds->times.renew_till) &&
249ebfedea0SLionel Sambuc 	    (mcreds->times.endtime <= creds->times.endtime);
250ebfedea0SLionel Sambuc 
251ebfedea0SLionel Sambuc     if (match && (whichfields & KRB5_TC_MATCH_AUTHDATA)) {
252ebfedea0SLionel Sambuc 	unsigned int i;
253ebfedea0SLionel Sambuc 	if(mcreds->authdata.len != creds->authdata.len)
254ebfedea0SLionel Sambuc 	    match = FALSE;
255ebfedea0SLionel Sambuc 	else
256ebfedea0SLionel Sambuc 	    for(i = 0; match && i < mcreds->authdata.len; i++)
257ebfedea0SLionel Sambuc 		match = (mcreds->authdata.val[i].ad_type ==
258ebfedea0SLionel Sambuc 			 creds->authdata.val[i].ad_type) &&
259ebfedea0SLionel Sambuc 		    (krb5_data_cmp(&mcreds->authdata.val[i].ad_data,
260ebfedea0SLionel Sambuc 				   &creds->authdata.val[i].ad_data) == 0);
261ebfedea0SLionel Sambuc     }
262ebfedea0SLionel Sambuc     if (match && (whichfields & KRB5_TC_MATCH_2ND_TKT))
263ebfedea0SLionel Sambuc 	match = (krb5_data_cmp(&mcreds->second_ticket, &creds->second_ticket) == 0);
264ebfedea0SLionel Sambuc 
265ebfedea0SLionel Sambuc     if (match && (whichfields & KRB5_TC_MATCH_IS_SKEY))
266ebfedea0SLionel Sambuc 	match = ((mcreds->second_ticket.length == 0) ==
267ebfedea0SLionel Sambuc 		 (creds->second_ticket.length == 0));
268ebfedea0SLionel Sambuc 
269ebfedea0SLionel Sambuc     return match;
270ebfedea0SLionel Sambuc }
271ebfedea0SLionel Sambuc 
272ebfedea0SLionel Sambuc /**
273ebfedea0SLionel Sambuc  * Returns the ticket flags for the credentials in creds.
274ebfedea0SLionel Sambuc  * See also krb5_ticket_get_flags().
275ebfedea0SLionel Sambuc  *
276ebfedea0SLionel Sambuc  * @param creds credential to get ticket flags from
277ebfedea0SLionel Sambuc  *
278ebfedea0SLionel Sambuc  * @return ticket flags
279ebfedea0SLionel Sambuc  *
280ebfedea0SLionel Sambuc  * @ingroup krb5
281ebfedea0SLionel Sambuc  */
282ebfedea0SLionel Sambuc 
283ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION unsigned long KRB5_LIB_CALL
krb5_creds_get_ticket_flags(krb5_creds * creds)284ebfedea0SLionel Sambuc krb5_creds_get_ticket_flags(krb5_creds *creds)
285ebfedea0SLionel Sambuc {
286ebfedea0SLionel Sambuc     return TicketFlags2int(creds->flags.b);
287ebfedea0SLionel Sambuc }
288