2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * 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 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
36 RCSID("$Id: creds.c 15167 2005-05-18 04:21:57Z lha $");
38 /* keep this for compatibility with older code */
39 krb5_error_code KRB5_LIB_FUNCTION
40 krb5_free_creds_contents (krb5_context context, krb5_creds *c)
42 return krb5_free_cred_contents (context, c);
45 krb5_error_code KRB5_LIB_FUNCTION
46 krb5_free_cred_contents (krb5_context context, krb5_creds *c)
48 krb5_free_principal (context, c->client);
50 krb5_free_principal (context, c->server);
52 krb5_free_keyblock_contents (context, &c->session);
53 krb5_data_free (&c->ticket);
54 krb5_data_free (&c->second_ticket);
55 free_AuthorizationData (&c->authdata);
56 krb5_free_addresses (context, &c->addresses);
57 memset(c, 0, sizeof(*c));
61 krb5_error_code KRB5_LIB_FUNCTION
62 krb5_copy_creds_contents (krb5_context context,
63 const krb5_creds *incred,
68 memset(c, 0, sizeof(*c));
69 ret = krb5_copy_principal (context, incred->client, &c->client);
72 ret = krb5_copy_principal (context, incred->server, &c->server);
75 ret = krb5_copy_keyblock_contents (context, &incred->session, &c->session);
78 c->times = incred->times;
79 ret = krb5_data_copy (&c->ticket,
81 incred->ticket.length);
84 ret = krb5_data_copy (&c->second_ticket,
85 incred->second_ticket.data,
86 incred->second_ticket.length);
89 ret = copy_AuthorizationData(&incred->authdata, &c->authdata);
92 ret = krb5_copy_addresses (context,
97 c->flags = incred->flags;
101 krb5_free_cred_contents (context, c);
105 krb5_error_code KRB5_LIB_FUNCTION
106 krb5_copy_creds (krb5_context context,
107 const krb5_creds *incred,
108 krb5_creds **outcred)
112 c = malloc (sizeof (*c));
114 krb5_set_error_string (context, "malloc: out of memory");
117 memset (c, 0, sizeof(*c));
119 return krb5_copy_creds_contents (context, incred, c);
122 krb5_error_code KRB5_LIB_FUNCTION
123 krb5_free_creds (krb5_context context, krb5_creds *c)
125 krb5_free_cred_contents (context, c);
130 /* XXX these do not belong here */
132 krb5_data_equal(const krb5_data *a, const krb5_data *b)
134 if(a->length != b->length)
136 return memcmp(a->data, b->data, a->length) == 0;
140 krb5_times_equal(const krb5_times *a, const krb5_times *b)
142 return a->starttime == b->starttime &&
143 a->authtime == b->authtime &&
144 a->endtime == b->endtime &&
145 a->renew_till == b->renew_till;
149 * Return TRUE if `mcreds' and `creds' are equal (`whichfields'
150 * determines what equal means).
153 krb5_boolean KRB5_LIB_FUNCTION
154 krb5_compare_creds(krb5_context context, krb5_flags whichfields,
155 const krb5_creds * mcreds, const krb5_creds * creds)
157 krb5_boolean match = TRUE;
159 if (match && mcreds->server) {
160 if (whichfields & (KRB5_TC_DONT_MATCH_REALM | KRB5_TC_MATCH_SRV_NAMEONLY))
161 match = krb5_principal_compare_any_realm (context, mcreds->server,
164 match = krb5_principal_compare (context, mcreds->server,
168 if (match && mcreds->client) {
169 if(whichfields & KRB5_TC_DONT_MATCH_REALM)
170 match = krb5_principal_compare_any_realm (context, mcreds->client,
173 match = krb5_principal_compare (context, mcreds->client,
177 if (match && (whichfields & KRB5_TC_MATCH_KEYTYPE))
178 match = krb5_enctypes_compatible_keys(context,
179 mcreds->session.keytype,
180 creds->session.keytype);
182 if (match && (whichfields & KRB5_TC_MATCH_FLAGS_EXACT))
183 match = mcreds->flags.i == creds->flags.i;
185 if (match && (whichfields & KRB5_TC_MATCH_FLAGS))
186 match = (creds->flags.i & mcreds->flags.i) == mcreds->flags.i;
188 if (match && (whichfields & KRB5_TC_MATCH_TIMES_EXACT))
189 match = krb5_times_equal(&mcreds->times, &creds->times);
191 if (match && (whichfields & KRB5_TC_MATCH_TIMES))
192 /* compare only expiration times */
193 match = (mcreds->times.renew_till <= creds->times.renew_till) &&
194 (mcreds->times.endtime <= creds->times.endtime);
196 if (match && (whichfields & KRB5_TC_MATCH_AUTHDATA)) {
198 if(mcreds->authdata.len != creds->authdata.len)
201 for(i = 0; match && i < mcreds->authdata.len; i++)
202 match = (mcreds->authdata.val[i].ad_type ==
203 creds->authdata.val[i].ad_type) &&
204 krb5_data_equal(&mcreds->authdata.val[i].ad_data,
205 &creds->authdata.val[i].ad_data);
207 if (match && (whichfields & KRB5_TC_MATCH_2ND_TKT))
208 match = krb5_data_equal(&mcreds->second_ticket, &creds->second_ticket);
210 if (match && (whichfields & KRB5_TC_MATCH_IS_SKEY))
211 match = ((mcreds->second_ticket.length == 0) ==
212 (creds->second_ticket.length == 0));