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