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