Home | History | Annotate | Line # | Download | only in krb5
      1  1.1     elric /*	$NetBSD: data.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 1997 - 2007 Kungliga Tekniska Hgskolan
      5  1.1     elric  * (Royal Institute of Technology, Stockholm, Sweden).
      6  1.1     elric  * All rights reserved.
      7  1.1     elric  *
      8  1.1     elric  * Redistribution and use in source and binary forms, with or without
      9  1.1     elric  * modification, are permitted provided that the following conditions
     10  1.1     elric  * are met:
     11  1.1     elric  *
     12  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     13  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     14  1.1     elric  *
     15  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     elric  *    documentation and/or other materials provided with the distribution.
     18  1.1     elric  *
     19  1.1     elric  * 3. Neither the name of the Institute nor the names of its contributors
     20  1.1     elric  *    may be used to endorse or promote products derived from this software
     21  1.1     elric  *    without specific prior written permission.
     22  1.1     elric  *
     23  1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     24  1.1     elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1     elric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     27  1.1     elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1     elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1     elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1     elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1     elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1     elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1     elric  * SUCH DAMAGE.
     34  1.1     elric  */
     35  1.1     elric 
     36  1.1     elric #include "krb5_locl.h"
     37  1.1     elric 
     38  1.1     elric /**
     39  1.1     elric  * Reset the (potentially uninitalized) krb5_data structure.
     40  1.1     elric  *
     41  1.1     elric  * @param p krb5_data to reset.
     42  1.1     elric  *
     43  1.1     elric  * @ingroup krb5
     44  1.1     elric  */
     45  1.1     elric 
     46  1.1     elric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
     47  1.1     elric krb5_data_zero(krb5_data *p)
     48  1.1     elric {
     49  1.1     elric     p->length = 0;
     50  1.1     elric     p->data   = NULL;
     51  1.1     elric }
     52  1.1     elric 
     53  1.1     elric /**
     54  1.1     elric  * Free the content of krb5_data structure, its ok to free a zeroed
     55  1.1     elric  * structure (with memset() or krb5_data_zero()). When done, the
     56  1.1     elric  * structure will be zeroed. The same function is called
     57  1.1     elric  * krb5_free_data_contents() in MIT Kerberos.
     58  1.1     elric  *
     59  1.1     elric  * @param p krb5_data to free.
     60  1.1     elric  *
     61  1.1     elric  * @ingroup krb5
     62  1.1     elric  */
     63  1.1     elric 
     64  1.1     elric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
     65  1.1     elric krb5_data_free(krb5_data *p)
     66  1.1     elric {
     67  1.2  christos     free(p->data);
     68  1.1     elric     krb5_data_zero(p);
     69  1.1     elric }
     70  1.1     elric 
     71  1.1     elric /**
     72  1.1     elric  * Free krb5_data (and its content).
     73  1.1     elric  *
     74  1.1     elric  * @param context Kerberos 5 context.
     75  1.1     elric  * @param p krb5_data to free.
     76  1.1     elric  *
     77  1.1     elric  * @ingroup krb5
     78  1.1     elric  */
     79  1.1     elric 
     80  1.1     elric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
     81  1.1     elric krb5_free_data(krb5_context context,
     82  1.1     elric 	       krb5_data *p)
     83  1.1     elric {
     84  1.1     elric     krb5_data_free(p);
     85  1.1     elric     free(p);
     86  1.1     elric }
     87  1.1     elric 
     88  1.1     elric /**
     89  1.1     elric  * Allocate data of and krb5_data.
     90  1.1     elric  *
     91  1.1     elric  * @param p krb5_data to allocate.
     92  1.1     elric  * @param len size to allocate.
     93  1.1     elric  *
     94  1.1     elric  * @return Returns 0 to indicate success. Otherwise an kerberos et
     95  1.1     elric  * error code is returned.
     96  1.1     elric  *
     97  1.1     elric  * @ingroup krb5
     98  1.1     elric  */
     99  1.1     elric 
    100  1.1     elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
    101  1.1     elric krb5_data_alloc(krb5_data *p, int len)
    102  1.1     elric {
    103  1.1     elric     p->data = malloc(len);
    104  1.1     elric     if(len && p->data == NULL)
    105  1.1     elric 	return ENOMEM;
    106  1.1     elric     p->length = len;
    107  1.1     elric     return 0;
    108  1.1     elric }
    109  1.1     elric 
    110  1.1     elric /**
    111  1.1     elric  * Grow (or shrink) the content of krb5_data to a new size.
    112  1.1     elric  *
    113  1.1     elric  * @param p krb5_data to free.
    114  1.1     elric  * @param len new size.
    115  1.1     elric  *
    116  1.1     elric  * @return Returns 0 to indicate success. Otherwise an kerberos et
    117  1.1     elric  * error code is returned.
    118  1.1     elric  *
    119  1.1     elric  * @ingroup krb5
    120  1.1     elric  */
    121  1.1     elric 
    122  1.1     elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
    123  1.1     elric krb5_data_realloc(krb5_data *p, int len)
    124  1.1     elric {
    125  1.1     elric     void *tmp;
    126  1.1     elric     tmp = realloc(p->data, len);
    127  1.1     elric     if(len && !tmp)
    128  1.1     elric 	return ENOMEM;
    129  1.1     elric     p->data = tmp;
    130  1.1     elric     p->length = len;
    131  1.1     elric     return 0;
    132  1.1     elric }
    133  1.1     elric 
    134  1.1     elric /**
    135  1.1     elric  * Copy the data of len into the krb5_data.
    136  1.1     elric  *
    137  1.1     elric  * @param p krb5_data to copy into.
    138  1.1     elric  * @param data data to copy..
    139  1.1     elric  * @param len new size.
    140  1.1     elric  *
    141  1.1     elric  * @return Returns 0 to indicate success. Otherwise an kerberos et
    142  1.1     elric  * error code is returned.
    143  1.1     elric  *
    144  1.1     elric  * @ingroup krb5
    145  1.1     elric  */
    146  1.1     elric 
    147  1.1     elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
    148  1.1     elric krb5_data_copy(krb5_data *p, const void *data, size_t len)
    149  1.1     elric {
    150  1.1     elric     if (len) {
    151  1.1     elric 	if(krb5_data_alloc(p, len))
    152  1.1     elric 	    return ENOMEM;
    153  1.1     elric 	memmove(p->data, data, len);
    154  1.1     elric     } else
    155  1.1     elric 	p->data = NULL;
    156  1.1     elric     p->length = len;
    157  1.1     elric     return 0;
    158  1.1     elric }
    159  1.1     elric 
    160  1.1     elric /**
    161  1.1     elric  * Copy the data into a newly allocated krb5_data.
    162  1.1     elric  *
    163  1.1     elric  * @param context Kerberos 5 context.
    164  1.1     elric  * @param indata the krb5_data data to copy
    165  1.1     elric  * @param outdata new krb5_date to copy too. Free with krb5_free_data().
    166  1.1     elric  *
    167  1.1     elric  * @return Returns 0 to indicate success. Otherwise an kerberos et
    168  1.1     elric  * error code is returned.
    169  1.1     elric  *
    170  1.1     elric  * @ingroup krb5
    171  1.1     elric  */
    172  1.1     elric 
    173  1.1     elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
    174  1.1     elric krb5_copy_data(krb5_context context,
    175  1.1     elric 	       const krb5_data *indata,
    176  1.1     elric 	       krb5_data **outdata)
    177  1.1     elric {
    178  1.1     elric     krb5_error_code ret;
    179  1.1     elric     ALLOC(*outdata, 1);
    180  1.2  christos     if(*outdata == NULL)
    181  1.2  christos 	return krb5_enomem(context);
    182  1.1     elric     ret = der_copy_octet_string(indata, *outdata);
    183  1.1     elric     if(ret) {
    184  1.1     elric 	krb5_clear_error_message (context);
    185  1.1     elric 	free(*outdata);
    186  1.1     elric 	*outdata = NULL;
    187  1.1     elric     }
    188  1.1     elric     return ret;
    189  1.1     elric }
    190  1.1     elric 
    191  1.1     elric /**
    192  1.1     elric  * Compare to data.
    193  1.1     elric  *
    194  1.1     elric  * @param data1 krb5_data to compare
    195  1.1     elric  * @param data2 krb5_data to compare
    196  1.1     elric  *
    197  1.1     elric  * @return return the same way as memcmp(), useful when sorting.
    198  1.1     elric  *
    199  1.1     elric  * @ingroup krb5
    200  1.1     elric  */
    201  1.1     elric 
    202  1.1     elric KRB5_LIB_FUNCTION int KRB5_LIB_CALL
    203  1.1     elric krb5_data_cmp(const krb5_data *data1, const krb5_data *data2)
    204  1.1     elric {
    205  1.1     elric     if (data1->length != data2->length)
    206  1.1     elric 	return data1->length - data2->length;
    207  1.1     elric     return memcmp(data1->data, data2->data, data1->length);
    208  1.1     elric }
    209  1.1     elric 
    210  1.1     elric /**
    211  1.1     elric  * Compare to data not exposing timing information from the checksum data
    212  1.1     elric  *
    213  1.1     elric  * @param data1 krb5_data to compare
    214  1.1     elric  * @param data2 krb5_data to compare
    215  1.1     elric  *
    216  1.1     elric  * @return returns zero for same data, otherwise non zero.
    217  1.1     elric  *
    218  1.1     elric  * @ingroup krb5
    219  1.1     elric  */
    220  1.1     elric 
    221  1.1     elric KRB5_LIB_FUNCTION int KRB5_LIB_CALL
    222  1.1     elric krb5_data_ct_cmp(const krb5_data *data1, const krb5_data *data2)
    223  1.1     elric {
    224  1.1     elric     if (data1->length != data2->length)
    225  1.1     elric 	return data1->length - data2->length;
    226  1.1     elric     return ct_memcmp(data1->data, data2->data, data1->length);
    227  1.1     elric }
    228