1 /* $NetBSD: creds.c,v 1.1.1.2 2014/04/24 12:45:49 pettai Exp $ */
4 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
39 * Free content of krb5_creds.
41 * @param context Kerberos 5 context.
42 * @param c krb5_creds to free.
44 * @return Returns 0 to indicate success. Otherwise an kerberos et
45 * error code is returned, see krb5_get_error_message().
50 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
51 krb5_free_cred_contents (krb5_context context
, krb5_creds
*c
)
53 krb5_free_principal (context
, c
->client
);
55 krb5_free_principal (context
, c
->server
);
57 krb5_free_keyblock_contents (context
, &c
->session
);
58 krb5_data_free (&c
->ticket
);
59 krb5_data_free (&c
->second_ticket
);
60 free_AuthorizationData (&c
->authdata
);
61 krb5_free_addresses (context
, &c
->addresses
);
62 memset(c
, 0, sizeof(*c
));
67 * Copy content of krb5_creds.
69 * @param context Kerberos 5 context.
70 * @param incred source credential
71 * @param c destination credential, free with krb5_free_cred_contents().
73 * @return Returns 0 to indicate success. Otherwise an kerberos et
74 * error code is returned, see krb5_get_error_message().
79 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
80 krb5_copy_creds_contents (krb5_context context
,
81 const krb5_creds
*incred
,
86 memset(c
, 0, sizeof(*c
));
87 ret
= krb5_copy_principal (context
, incred
->client
, &c
->client
);
90 ret
= krb5_copy_principal (context
, incred
->server
, &c
->server
);
93 ret
= krb5_copy_keyblock_contents (context
, &incred
->session
, &c
->session
);
96 c
->times
= incred
->times
;
97 ret
= krb5_data_copy (&c
->ticket
,
99 incred
->ticket
.length
);
102 ret
= krb5_data_copy (&c
->second_ticket
,
103 incred
->second_ticket
.data
,
104 incred
->second_ticket
.length
);
107 ret
= copy_AuthorizationData(&incred
->authdata
, &c
->authdata
);
110 ret
= krb5_copy_addresses (context
,
115 c
->flags
= incred
->flags
;
119 krb5_free_cred_contents (context
, c
);
126 * @param context Kerberos 5 context.
127 * @param incred source credential
128 * @param outcred destination credential, free with krb5_free_creds().
130 * @return Returns 0 to indicate success. Otherwise an kerberos et
131 * error code is returned, see krb5_get_error_message().
136 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
137 krb5_copy_creds (krb5_context context
,
138 const krb5_creds
*incred
,
139 krb5_creds
**outcred
)
143 c
= malloc (sizeof (*c
));
145 krb5_set_error_message (context
, ENOMEM
,
146 N_("malloc: out of memory", ""));
149 memset (c
, 0, sizeof(*c
));
151 return krb5_copy_creds_contents (context
, incred
, c
);
157 * @param context Kerberos 5 context.
158 * @param c krb5_creds to free.
160 * @return Returns 0 to indicate success. Otherwise an kerberos et
161 * error code is returned, see krb5_get_error_message().
166 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
167 krb5_free_creds (krb5_context context
, krb5_creds
*c
)
169 krb5_free_cred_contents (context
, c
);
174 /* XXX this do not belong here */
176 krb5_times_equal(const krb5_times
*a
, const krb5_times
*b
)
178 return a
->starttime
== b
->starttime
&&
179 a
->authtime
== b
->authtime
&&
180 a
->endtime
== b
->endtime
&&
181 a
->renew_till
== b
->renew_till
;
185 * Return TRUE if `mcreds' and `creds' are equal (`whichfields'
186 * determines what equal means).
189 * The following flags, set in whichfields affects the comparison:
190 * - KRB5_TC_MATCH_SRV_NAMEONLY Consider all realms equal when comparing the service principal.
191 * - KRB5_TC_MATCH_KEYTYPE Compare enctypes.
192 * - KRB5_TC_MATCH_FLAGS_EXACT Make sure that the ticket flags are identical.
193 * - KRB5_TC_MATCH_FLAGS Make sure that all ticket flags set in mcreds are also present in creds .
194 * - KRB5_TC_MATCH_TIMES_EXACT Compares the ticket times exactly.
195 * - KRB5_TC_MATCH_TIMES Compares only the expiration times of the creds.
196 * - KRB5_TC_MATCH_AUTHDATA Compares the authdata fields.
197 * - KRB5_TC_MATCH_2ND_TKT Compares the second tickets (used by user-to-user authentication).
198 * - KRB5_TC_MATCH_IS_SKEY Compares the existance of the second ticket.
200 * @param context Kerberos 5 context.
201 * @param whichfields which fields to compare.
202 * @param mcreds cred to compare with.
203 * @param creds cred to compare with.
205 * @return return TRUE if mcred and creds are equal, FALSE if not.
210 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
211 krb5_compare_creds(krb5_context context
, krb5_flags whichfields
,
212 const krb5_creds
* mcreds
, const krb5_creds
* creds
)
214 krb5_boolean match
= TRUE
;
216 if (match
&& mcreds
->server
) {
217 if (whichfields
& (KRB5_TC_DONT_MATCH_REALM
| KRB5_TC_MATCH_SRV_NAMEONLY
))
218 match
= krb5_principal_compare_any_realm (context
, mcreds
->server
,
221 match
= krb5_principal_compare (context
, mcreds
->server
,
225 if (match
&& mcreds
->client
) {
226 if(whichfields
& KRB5_TC_DONT_MATCH_REALM
)
227 match
= krb5_principal_compare_any_realm (context
, mcreds
->client
,
230 match
= krb5_principal_compare (context
, mcreds
->client
,
234 if (match
&& (whichfields
& KRB5_TC_MATCH_KEYTYPE
))
235 match
= mcreds
->session
.keytype
== 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
)) {
253 if(mcreds
->authdata
.len
!= creds
->authdata
.len
)
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));
273 * Returns the ticket flags for the credentials in creds.
274 * See also krb5_ticket_get_flags().
276 * @param creds credential to get ticket flags from
278 * @return ticket flags
283 KRB5_LIB_FUNCTION
unsigned long KRB5_LIB_CALL
284 krb5_creds_get_ticket_flags(krb5_creds
*creds
)
286 return TicketFlags2int(creds
->flags
.b
);