No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / creds.c
blobd7a77ba9e1a846551062487c6101609c77b8e2b9
1 /*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
36 __RCSID("$Heimdal: creds.c 22062 2007-11-11 15:41:50Z lha $"
37 "$NetBSD$");
39 #undef __attribute__
40 #define __attribute__(X)
42 /* keep this for compatibility with older code */
43 krb5_error_code KRB5_LIB_FUNCTION __attribute__((deprecated))
44 krb5_free_creds_contents (krb5_context context, krb5_creds *c)
46 return krb5_free_cred_contents (context, c);
49 /**
50 * Free content of krb5_creds.
52 * @param context Kerberos 5 context.
53 * @param c krb5_creds to free.
55 * @return Returns 0 to indicate success. Otherwise an kerberos et
56 * error code is returned, see krb5_get_error_message().
58 * @ingroup krb5
61 krb5_error_code KRB5_LIB_FUNCTION
62 krb5_free_cred_contents (krb5_context context, krb5_creds *c)
64 krb5_free_principal (context, c->client);
65 c->client = NULL;
66 krb5_free_principal (context, c->server);
67 c->server = NULL;
68 krb5_free_keyblock_contents (context, &c->session);
69 krb5_data_free (&c->ticket);
70 krb5_data_free (&c->second_ticket);
71 free_AuthorizationData (&c->authdata);
72 krb5_free_addresses (context, &c->addresses);
73 memset(c, 0, sizeof(*c));
74 return 0;
77 /**
78 * Copy content of krb5_creds.
80 * @param context Kerberos 5 context.
81 * @param incred source credential
82 * @param c destination credential, free with krb5_free_cred_contents().
84 * @return Returns 0 to indicate success. Otherwise an kerberos et
85 * error code is returned, see krb5_get_error_message().
87 * @ingroup krb5
90 krb5_error_code KRB5_LIB_FUNCTION
91 krb5_copy_creds_contents (krb5_context context,
92 const krb5_creds *incred,
93 krb5_creds *c)
95 krb5_error_code ret;
97 memset(c, 0, sizeof(*c));
98 ret = krb5_copy_principal (context, incred->client, &c->client);
99 if (ret)
100 goto fail;
101 ret = krb5_copy_principal (context, incred->server, &c->server);
102 if (ret)
103 goto fail;
104 ret = krb5_copy_keyblock_contents (context, &incred->session, &c->session);
105 if (ret)
106 goto fail;
107 c->times = incred->times;
108 ret = krb5_data_copy (&c->ticket,
109 incred->ticket.data,
110 incred->ticket.length);
111 if (ret)
112 goto fail;
113 ret = krb5_data_copy (&c->second_ticket,
114 incred->second_ticket.data,
115 incred->second_ticket.length);
116 if (ret)
117 goto fail;
118 ret = copy_AuthorizationData(&incred->authdata, &c->authdata);
119 if (ret)
120 goto fail;
121 ret = krb5_copy_addresses (context,
122 &incred->addresses,
123 &c->addresses);
124 if (ret)
125 goto fail;
126 c->flags = incred->flags;
127 return 0;
129 fail:
130 krb5_free_cred_contents (context, c);
131 return ret;
135 * Copy krb5_creds.
137 * @param context Kerberos 5 context.
138 * @param incred source credential
139 * @param outcred destination credential, free with krb5_free_creds().
141 * @return Returns 0 to indicate success. Otherwise an kerberos et
142 * error code is returned, see krb5_get_error_message().
144 * @ingroup krb5
147 krb5_error_code KRB5_LIB_FUNCTION
148 krb5_copy_creds (krb5_context context,
149 const krb5_creds *incred,
150 krb5_creds **outcred)
152 krb5_creds *c;
154 c = malloc (sizeof (*c));
155 if (c == NULL) {
156 krb5_set_error_string (context, "malloc: out of memory");
157 return ENOMEM;
159 memset (c, 0, sizeof(*c));
160 *outcred = c;
161 return krb5_copy_creds_contents (context, incred, c);
165 * Free krb5_creds.
167 * @param context Kerberos 5 context.
168 * @param c krb5_creds to free.
170 * @return Returns 0 to indicate success. Otherwise an kerberos et
171 * error code is returned, see krb5_get_error_message().
173 * @ingroup krb5
176 krb5_error_code KRB5_LIB_FUNCTION
177 krb5_free_creds (krb5_context context, krb5_creds *c)
179 krb5_free_cred_contents (context, c);
180 free (c);
181 return 0;
184 /* XXX this do not belong here */
185 static krb5_boolean
186 krb5_times_equal(const krb5_times *a, const krb5_times *b)
188 return a->starttime == b->starttime &&
189 a->authtime == b->authtime &&
190 a->endtime == b->endtime &&
191 a->renew_till == b->renew_till;
195 * Return TRUE if `mcreds' and `creds' are equal (`whichfields'
196 * determines what equal means).
198 * @param context Kerberos 5 context.
199 * @param whichfields which fields to compare.
200 * @param mcreds cred to compare with.
201 * @param creds cred to compare with.
203 * @return return TRUE if mcred and creds are equal, FALSE if not.
205 * @ingroup krb5
208 krb5_boolean KRB5_LIB_FUNCTION
209 krb5_compare_creds(krb5_context context, krb5_flags whichfields,
210 const krb5_creds * mcreds, const krb5_creds * creds)
212 krb5_boolean match = TRUE;
214 if (match && mcreds->server) {
215 if (whichfields & (KRB5_TC_DONT_MATCH_REALM | KRB5_TC_MATCH_SRV_NAMEONLY))
216 match = krb5_principal_compare_any_realm (context, mcreds->server,
217 creds->server);
218 else
219 match = krb5_principal_compare (context, mcreds->server,
220 creds->server);
223 if (match && mcreds->client) {
224 if(whichfields & KRB5_TC_DONT_MATCH_REALM)
225 match = krb5_principal_compare_any_realm (context, mcreds->client,
226 creds->client);
227 else
228 match = krb5_principal_compare (context, mcreds->client,
229 creds->client);
232 if (match && (whichfields & KRB5_TC_MATCH_KEYTYPE))
233 match = krb5_enctypes_compatible_keys(context,
234 mcreds->session.keytype,
235 creds->session.keytype);
237 if (match && (whichfields & KRB5_TC_MATCH_FLAGS_EXACT))
238 match = mcreds->flags.i == creds->flags.i;
240 if (match && (whichfields & KRB5_TC_MATCH_FLAGS))
241 match = (creds->flags.i & mcreds->flags.i) == mcreds->flags.i;
243 if (match && (whichfields & KRB5_TC_MATCH_TIMES_EXACT))
244 match = krb5_times_equal(&mcreds->times, &creds->times);
246 if (match && (whichfields & KRB5_TC_MATCH_TIMES))
247 /* compare only expiration times */
248 match = (mcreds->times.renew_till <= creds->times.renew_till) &&
249 (mcreds->times.endtime <= creds->times.endtime);
251 if (match && (whichfields & KRB5_TC_MATCH_AUTHDATA)) {
252 unsigned int i;
253 if(mcreds->authdata.len != creds->authdata.len)
254 match = FALSE;
255 else
256 for(i = 0; match && i < mcreds->authdata.len; i++)
257 match = (mcreds->authdata.val[i].ad_type ==
258 creds->authdata.val[i].ad_type) &&
259 (krb5_data_cmp(&mcreds->authdata.val[i].ad_data,
260 &creds->authdata.val[i].ad_data) == 0);
262 if (match && (whichfields & KRB5_TC_MATCH_2ND_TKT))
263 match = (krb5_data_cmp(&mcreds->second_ticket, &creds->second_ticket) == 0);
265 if (match && (whichfields & KRB5_TC_MATCH_IS_SKEY))
266 match = ((mcreds->second_ticket.length == 0) ==
267 (creds->second_ticket.length == 0));
269 return match;