/*1* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "krb5_locl.h"3435/**36* Free content of krb5_creds.37*38* @param context Kerberos 5 context.39* @param c krb5_creds to free.40*41* @return Returns 0 to indicate success. Otherwise an kerberos et42* error code is returned, see krb5_get_error_message().43*44* @ingroup krb545*/4647KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL48krb5_free_cred_contents (krb5_context context, krb5_creds *c)49{50krb5_free_principal (context, c->client);51c->client = NULL;52krb5_free_principal (context, c->server);53c->server = NULL;54krb5_free_keyblock_contents (context, &c->session);55krb5_data_free (&c->ticket);56krb5_data_free (&c->second_ticket);57free_AuthorizationData (&c->authdata);58krb5_free_addresses (context, &c->addresses);59memset(c, 0, sizeof(*c));60return 0;61}6263/**64* Copy content of krb5_creds.65*66* @param context Kerberos 5 context.67* @param incred source credential68* @param c destination credential, free with krb5_free_cred_contents().69*70* @return Returns 0 to indicate success. Otherwise an kerberos et71* error code is returned, see krb5_get_error_message().72*73* @ingroup krb574*/7576KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL77krb5_copy_creds_contents (krb5_context context,78const krb5_creds *incred,79krb5_creds *c)80{81krb5_error_code ret;8283memset(c, 0, sizeof(*c));84ret = krb5_copy_principal (context, incred->client, &c->client);85if (ret)86goto fail;87ret = krb5_copy_principal (context, incred->server, &c->server);88if (ret)89goto fail;90ret = krb5_copy_keyblock_contents (context, &incred->session, &c->session);91if (ret)92goto fail;93c->times = incred->times;94ret = krb5_data_copy (&c->ticket,95incred->ticket.data,96incred->ticket.length);97if (ret)98goto fail;99ret = krb5_data_copy (&c->second_ticket,100incred->second_ticket.data,101incred->second_ticket.length);102if (ret)103goto fail;104ret = copy_AuthorizationData(&incred->authdata, &c->authdata);105if (ret)106goto fail;107ret = krb5_copy_addresses (context,108&incred->addresses,109&c->addresses);110if (ret)111goto fail;112c->flags = incred->flags;113return 0;114115fail:116krb5_free_cred_contents (context, c);117return ret;118}119120/**121* Copy krb5_creds.122*123* @param context Kerberos 5 context.124* @param incred source credential125* @param outcred destination credential, free with krb5_free_creds().126*127* @return Returns 0 to indicate success. Otherwise an kerberos et128* error code is returned, see krb5_get_error_message().129*130* @ingroup krb5131*/132133KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL134krb5_copy_creds (krb5_context context,135const krb5_creds *incred,136krb5_creds **outcred)137{138krb5_creds *c;139140c = malloc (sizeof (*c));141if (c == NULL) {142krb5_set_error_message (context, ENOMEM,143N_("malloc: out of memory", ""));144return ENOMEM;145}146memset (c, 0, sizeof(*c));147*outcred = c;148return krb5_copy_creds_contents (context, incred, c);149}150151/**152* Free krb5_creds.153*154* @param context Kerberos 5 context.155* @param c krb5_creds to free.156*157* @return Returns 0 to indicate success. Otherwise an kerberos et158* error code is returned, see krb5_get_error_message().159*160* @ingroup krb5161*/162163KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL164krb5_free_creds (krb5_context context, krb5_creds *c)165{166krb5_free_cred_contents (context, c);167free (c);168return 0;169}170171/* XXX this do not belong here */172static krb5_boolean173krb5_times_equal(const krb5_times *a, const krb5_times *b)174{175return a->starttime == b->starttime &&176a->authtime == b->authtime &&177a->endtime == b->endtime &&178a->renew_till == b->renew_till;179}180181/**182* Return TRUE if `mcreds' and `creds' are equal (`whichfields'183* determines what equal means).184*185*186* The following flags, set in whichfields affects the comparison:187* - KRB5_TC_MATCH_SRV_NAMEONLY Consider all realms equal when comparing the service principal.188* - KRB5_TC_MATCH_KEYTYPE Compare enctypes.189* - KRB5_TC_MATCH_FLAGS_EXACT Make sure that the ticket flags are identical.190* - KRB5_TC_MATCH_FLAGS Make sure that all ticket flags set in mcreds are also present in creds .191* - KRB5_TC_MATCH_TIMES_EXACT Compares the ticket times exactly.192* - KRB5_TC_MATCH_TIMES Compares only the expiration times of the creds.193* - KRB5_TC_MATCH_AUTHDATA Compares the authdata fields.194* - KRB5_TC_MATCH_2ND_TKT Compares the second tickets (used by user-to-user authentication).195* - KRB5_TC_MATCH_IS_SKEY Compares the existance of the second ticket.196*197* @param context Kerberos 5 context.198* @param whichfields which fields to compare.199* @param mcreds cred to compare with.200* @param creds cred to compare with.201*202* @return return TRUE if mcred and creds are equal, FALSE if not.203*204* @ingroup krb5205*/206207KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL208krb5_compare_creds(krb5_context context, krb5_flags whichfields,209const krb5_creds * mcreds, const krb5_creds * creds)210{211krb5_boolean match = TRUE;212213if (match && mcreds->server) {214if (whichfields & (KRB5_TC_DONT_MATCH_REALM | KRB5_TC_MATCH_SRV_NAMEONLY))215match = krb5_principal_compare_any_realm (context, mcreds->server,216creds->server);217else218match = krb5_principal_compare (context, mcreds->server,219creds->server);220}221222if (match && mcreds->client) {223if(whichfields & KRB5_TC_DONT_MATCH_REALM)224match = krb5_principal_compare_any_realm (context, mcreds->client,225creds->client);226else227match = krb5_principal_compare (context, mcreds->client,228creds->client);229}230231if (match && (whichfields & KRB5_TC_MATCH_KEYTYPE))232match = mcreds->session.keytype == creds->session.keytype;233234if (match && (whichfields & KRB5_TC_MATCH_FLAGS_EXACT))235match = mcreds->flags.i == creds->flags.i;236237if (match && (whichfields & KRB5_TC_MATCH_FLAGS))238match = (creds->flags.i & mcreds->flags.i) == mcreds->flags.i;239240if (match && (whichfields & KRB5_TC_MATCH_TIMES_EXACT))241match = krb5_times_equal(&mcreds->times, &creds->times);242243if (match && (whichfields & KRB5_TC_MATCH_TIMES))244/* compare only expiration times */245match = (mcreds->times.renew_till <= creds->times.renew_till) &&246(mcreds->times.endtime <= creds->times.endtime);247248if (match && (whichfields & KRB5_TC_MATCH_AUTHDATA)) {249unsigned int i;250if(mcreds->authdata.len != creds->authdata.len)251match = FALSE;252else253for(i = 0; match && i < mcreds->authdata.len; i++)254match = (mcreds->authdata.val[i].ad_type ==255creds->authdata.val[i].ad_type) &&256(krb5_data_cmp(&mcreds->authdata.val[i].ad_data,257&creds->authdata.val[i].ad_data) == 0);258}259if (match && (whichfields & KRB5_TC_MATCH_2ND_TKT))260match = (krb5_data_cmp(&mcreds->second_ticket, &creds->second_ticket) == 0);261262if (match && (whichfields & KRB5_TC_MATCH_IS_SKEY))263match = ((mcreds->second_ticket.length == 0) ==264(creds->second_ticket.length == 0));265266return match;267}268269/**270* Returns the ticket flags for the credentials in creds.271* See also krb5_ticket_get_flags().272*273* @param creds credential to get ticket flags from274*275* @return ticket flags276*277* @ingroup krb5278*/279280KRB5_LIB_FUNCTION unsigned long KRB5_LIB_CALL281krb5_creds_get_ticket_flags(krb5_creds *creds)282{283return TicketFlags2int(creds->flags.b);284}285286287