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