/*1* Copyright (c) 1997 - 2007 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* Reset the (potentially uninitalized) krb5_data structure.37*38* @param p krb5_data to reset.39*40* @ingroup krb541*/4243KRB5_LIB_FUNCTION void KRB5_LIB_CALL44krb5_data_zero(krb5_data *p)45{46p->length = 0;47p->data = NULL;48}4950/**51* Free the content of krb5_data structure, its ok to free a zeroed52* structure (with memset() or krb5_data_zero()). When done, the53* structure will be zeroed. The same function is called54* krb5_free_data_contents() in MIT Kerberos.55*56* @param p krb5_data to free.57*58* @ingroup krb559*/6061KRB5_LIB_FUNCTION void KRB5_LIB_CALL62krb5_data_free(krb5_data *p)63{64if(p->data != NULL)65free(p->data);66krb5_data_zero(p);67}6869/**70* Free krb5_data (and its content).71*72* @param context Kerberos 5 context.73* @param p krb5_data to free.74*75* @ingroup krb576*/7778KRB5_LIB_FUNCTION void KRB5_LIB_CALL79krb5_free_data(krb5_context context,80krb5_data *p)81{82krb5_data_free(p);83free(p);84}8586/**87* Allocate data of and krb5_data.88*89* @param p krb5_data to allocate.90* @param len size to allocate.91*92* @return Returns 0 to indicate success. Otherwise an kerberos et93* error code is returned.94*95* @ingroup krb596*/9798KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL99krb5_data_alloc(krb5_data *p, int len)100{101p->data = malloc(len);102if(len && p->data == NULL)103return ENOMEM;104p->length = len;105return 0;106}107108/**109* Grow (or shrink) the content of krb5_data to a new size.110*111* @param p krb5_data to free.112* @param len new size.113*114* @return Returns 0 to indicate success. Otherwise an kerberos et115* error code is returned.116*117* @ingroup krb5118*/119120KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL121krb5_data_realloc(krb5_data *p, int len)122{123void *tmp;124tmp = realloc(p->data, len);125if(len && !tmp)126return ENOMEM;127p->data = tmp;128p->length = len;129return 0;130}131132/**133* Copy the data of len into the krb5_data.134*135* @param p krb5_data to copy into.136* @param data data to copy..137* @param len new size.138*139* @return Returns 0 to indicate success. Otherwise an kerberos et140* error code is returned.141*142* @ingroup krb5143*/144145KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL146krb5_data_copy(krb5_data *p, const void *data, size_t len)147{148if (len) {149if(krb5_data_alloc(p, len))150return ENOMEM;151memmove(p->data, data, len);152} else153p->data = NULL;154p->length = len;155return 0;156}157158/**159* Copy the data into a newly allocated krb5_data.160*161* @param context Kerberos 5 context.162* @param indata the krb5_data data to copy163* @param outdata new krb5_date to copy too. Free with krb5_free_data().164*165* @return Returns 0 to indicate success. Otherwise an kerberos et166* error code is returned.167*168* @ingroup krb5169*/170171KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL172krb5_copy_data(krb5_context context,173const krb5_data *indata,174krb5_data **outdata)175{176krb5_error_code ret;177ALLOC(*outdata, 1);178if(*outdata == NULL) {179krb5_set_error_message(context, ENOMEM, "malloc: out of memory");180return ENOMEM;181}182ret = der_copy_octet_string(indata, *outdata);183if(ret) {184krb5_clear_error_message (context);185free(*outdata);186*outdata = NULL;187}188return ret;189}190191/**192* Compare to data.193*194* @param data1 krb5_data to compare195* @param data2 krb5_data to compare196*197* @return return the same way as memcmp(), useful when sorting.198*199* @ingroup krb5200*/201202KRB5_LIB_FUNCTION int KRB5_LIB_CALL203krb5_data_cmp(const krb5_data *data1, const krb5_data *data2)204{205if (data1->length != data2->length)206return data1->length - data2->length;207return memcmp(data1->data, data2->data, data1->length);208}209210/**211* Compare to data not exposing timing information from the checksum data212*213* @param data1 krb5_data to compare214* @param data2 krb5_data to compare215*216* @return returns zero for same data, otherwise non zero.217*218* @ingroup krb5219*/220221KRB5_LIB_FUNCTION int KRB5_LIB_CALL222krb5_data_ct_cmp(const krb5_data *data1, const krb5_data *data2)223{224if (data1->length != data2->length)225return data1->length - data2->length;226return ct_memcmp(data1->data, data2->data, data1->length);227}228229230