1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/stack.h>
58
59 #include <assert.h>
60
61 #include <openssl/mem.h>
62
63 #include "../internal.h"
64
65
66 // kMinSize is the number of pointers that will be initially allocated in a new
67 // stack.
68 static const size_t kMinSize = 4;
69
sk_new(OPENSSL_sk_cmp_func comp)70 _STACK *sk_new(OPENSSL_sk_cmp_func comp) {
71 _STACK *ret = OPENSSL_malloc(sizeof(_STACK));
72 if (ret == NULL) {
73 return NULL;
74 }
75 OPENSSL_memset(ret, 0, sizeof(_STACK));
76
77 ret->data = OPENSSL_malloc(sizeof(void *) * kMinSize);
78 if (ret->data == NULL) {
79 goto err;
80 }
81
82 OPENSSL_memset(ret->data, 0, sizeof(void *) * kMinSize);
83
84 ret->comp = comp;
85 ret->num_alloc = kMinSize;
86
87 return ret;
88
89 err:
90 OPENSSL_free(ret);
91 return NULL;
92 }
93
sk_new_null(void)94 _STACK *sk_new_null(void) { return sk_new(NULL); }
95
sk_num(const _STACK * sk)96 size_t sk_num(const _STACK *sk) {
97 if (sk == NULL) {
98 return 0;
99 }
100 return sk->num;
101 }
102
sk_zero(_STACK * sk)103 void sk_zero(_STACK *sk) {
104 if (sk == NULL || sk->num == 0) {
105 return;
106 }
107 OPENSSL_memset(sk->data, 0, sizeof(void*) * sk->num);
108 sk->num = 0;
109 sk->sorted = 0;
110 }
111
sk_value(const _STACK * sk,size_t i)112 void *sk_value(const _STACK *sk, size_t i) {
113 if (!sk || i >= sk->num) {
114 return NULL;
115 }
116 return sk->data[i];
117 }
118
sk_set(_STACK * sk,size_t i,void * value)119 void *sk_set(_STACK *sk, size_t i, void *value) {
120 if (!sk || i >= sk->num) {
121 return NULL;
122 }
123 return sk->data[i] = value;
124 }
125
sk_free(_STACK * sk)126 void sk_free(_STACK *sk) {
127 if (sk == NULL) {
128 return;
129 }
130 OPENSSL_free(sk->data);
131 OPENSSL_free(sk);
132 }
133
sk_pop_free_ex(_STACK * sk,OPENSSL_sk_call_free_func call_free_func,OPENSSL_sk_free_func free_func)134 void sk_pop_free_ex(_STACK *sk, OPENSSL_sk_call_free_func call_free_func,
135 OPENSSL_sk_free_func free_func) {
136 if (sk == NULL) {
137 return;
138 }
139
140 for (size_t i = 0; i < sk->num; i++) {
141 if (sk->data[i] != NULL) {
142 call_free_func(free_func, sk->data[i]);
143 }
144 }
145 sk_free(sk);
146 }
147
148 // Historically, |sk_pop_free| called the function as |OPENSSL_sk_free_func|
149 // directly. This is undefined in C. Some callers called |sk_pop_free| directly,
150 // so we must maintain a compatibility version for now.
call_free_func_legacy(OPENSSL_sk_free_func func,void * ptr)151 static void call_free_func_legacy(OPENSSL_sk_free_func func, void *ptr) {
152 func(ptr);
153 }
154
sk_pop_free(_STACK * sk,OPENSSL_sk_free_func free_func)155 void sk_pop_free(_STACK *sk, OPENSSL_sk_free_func free_func) {
156 sk_pop_free_ex(sk, call_free_func_legacy, free_func);
157 }
158
sk_insert(_STACK * sk,void * p,size_t where)159 size_t sk_insert(_STACK *sk, void *p, size_t where) {
160 if (sk == NULL) {
161 return 0;
162 }
163
164 if (sk->num_alloc <= sk->num + 1) {
165 // Attempt to double the size of the array.
166 size_t new_alloc = sk->num_alloc << 1;
167 size_t alloc_size = new_alloc * sizeof(void *);
168 void **data;
169
170 // If the doubling overflowed, try to increment.
171 if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) {
172 new_alloc = sk->num_alloc + 1;
173 alloc_size = new_alloc * sizeof(void *);
174 }
175
176 // If the increment also overflowed, fail.
177 if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) {
178 return 0;
179 }
180
181 data = OPENSSL_realloc(sk->data, alloc_size);
182 if (data == NULL) {
183 return 0;
184 }
185
186 sk->data = data;
187 sk->num_alloc = new_alloc;
188 }
189
190 if (where >= sk->num) {
191 sk->data[sk->num] = p;
192 } else {
193 OPENSSL_memmove(&sk->data[where + 1], &sk->data[where],
194 sizeof(void *) * (sk->num - where));
195 sk->data[where] = p;
196 }
197
198 sk->num++;
199 sk->sorted = 0;
200
201 return sk->num;
202 }
203
sk_delete(_STACK * sk,size_t where)204 void *sk_delete(_STACK *sk, size_t where) {
205 void *ret;
206
207 if (!sk || where >= sk->num) {
208 return NULL;
209 }
210
211 ret = sk->data[where];
212
213 if (where != sk->num - 1) {
214 OPENSSL_memmove(&sk->data[where], &sk->data[where + 1],
215 sizeof(void *) * (sk->num - where - 1));
216 }
217
218 sk->num--;
219 return ret;
220 }
221
sk_delete_ptr(_STACK * sk,const void * p)222 void *sk_delete_ptr(_STACK *sk, const void *p) {
223 if (sk == NULL) {
224 return NULL;
225 }
226
227 for (size_t i = 0; i < sk->num; i++) {
228 if (sk->data[i] == p) {
229 return sk_delete(sk, i);
230 }
231 }
232
233 return NULL;
234 }
235
sk_delete_if(_STACK * sk,OPENSSL_sk_call_delete_if_func call_func,OPENSSL_sk_delete_if_func func,void * data)236 void sk_delete_if(_STACK *sk, OPENSSL_sk_call_delete_if_func call_func,
237 OPENSSL_sk_delete_if_func func, void *data) {
238 if (sk == NULL) {
239 return;
240 }
241
242 size_t new_num = 0;
243 for (size_t i = 0; i < sk->num; i++) {
244 if (!call_func(func, sk->data[i], data)) {
245 sk->data[new_num] = sk->data[i];
246 new_num++;
247 }
248 }
249 sk->num = new_num;
250 }
251
sk_find(const _STACK * sk,size_t * out_index,const void * p,OPENSSL_sk_call_cmp_func call_cmp_func)252 int sk_find(const _STACK *sk, size_t *out_index, const void *p,
253 OPENSSL_sk_call_cmp_func call_cmp_func) {
254 if (sk == NULL) {
255 return 0;
256 }
257
258 if (sk->comp == NULL) {
259 // Use pointer equality when no comparison function has been set.
260 for (size_t i = 0; i < sk->num; i++) {
261 if (sk->data[i] == p) {
262 if (out_index) {
263 *out_index = i;
264 }
265 return 1;
266 }
267 }
268 return 0;
269 }
270
271 if (p == NULL) {
272 return 0;
273 }
274
275 if (!sk_is_sorted(sk)) {
276 for (size_t i = 0; i < sk->num; i++) {
277 const void *elem = sk->data[i];
278 if (call_cmp_func(sk->comp, &p, &elem) == 0) {
279 if (out_index) {
280 *out_index = i;
281 }
282 return 1;
283 }
284 }
285 return 0;
286 }
287
288 // The stack is sorted, so binary search to find the element.
289 //
290 // |lo| and |hi| maintain a half-open interval of where the answer may be. All
291 // indices such that |lo <= idx < hi| are candidates.
292 size_t lo = 0, hi = sk->num;
293 while (lo < hi) {
294 // Bias |mid| towards |lo|. See the |r == 0| case below.
295 size_t mid = lo + (hi - lo - 1) / 2;
296 assert(lo <= mid && mid < hi);
297 const void *elem = sk->data[mid];
298 int r = call_cmp_func(sk->comp, &p, &elem);
299 if (r > 0) {
300 lo = mid + 1; // |mid| is too low.
301 } else if (r < 0) {
302 hi = mid; // |mid| is too high.
303 } else {
304 // |mid| matches. However, this function returns the earliest match, so we
305 // can only return if the range has size one.
306 if (hi - lo == 1) {
307 if (out_index != NULL) {
308 *out_index = mid;
309 }
310 return 1;
311 }
312 // The sample is biased towards |lo|. |mid| can only be |hi - 1| if
313 // |hi - lo| was one, so this makes forward progress.
314 assert(mid + 1 < hi);
315 hi = mid + 1;
316 }
317 }
318
319 assert(lo == hi);
320 return 0; // Not found.
321 }
322
sk_shift(_STACK * sk)323 void *sk_shift(_STACK *sk) {
324 if (sk == NULL) {
325 return NULL;
326 }
327 if (sk->num == 0) {
328 return NULL;
329 }
330 return sk_delete(sk, 0);
331 }
332
sk_push(_STACK * sk,void * p)333 size_t sk_push(_STACK *sk, void *p) { return (sk_insert(sk, p, sk->num)); }
334
sk_pop(_STACK * sk)335 void *sk_pop(_STACK *sk) {
336 if (sk == NULL) {
337 return NULL;
338 }
339 if (sk->num == 0) {
340 return NULL;
341 }
342 return sk_delete(sk, sk->num - 1);
343 }
344
sk_dup(const _STACK * sk)345 _STACK *sk_dup(const _STACK *sk) {
346 if (sk == NULL) {
347 return NULL;
348 }
349
350 _STACK *ret = OPENSSL_malloc(sizeof(_STACK));
351 if (ret == NULL) {
352 return NULL;
353 }
354 OPENSSL_memset(ret, 0, sizeof(_STACK));
355
356 ret->data = OPENSSL_malloc(sizeof(void *) * sk->num_alloc);
357 if (ret->data == NULL) {
358 goto err;
359 }
360
361 ret->num = sk->num;
362 OPENSSL_memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
363 ret->sorted = sk->sorted;
364 ret->num_alloc = sk->num_alloc;
365 ret->comp = sk->comp;
366 return ret;
367
368 err:
369 sk_free(ret);
370 return NULL;
371 }
372
373 #if defined(_MSC_VER)
374 struct sort_compare_ctx {
375 OPENSSL_sk_call_cmp_func call_cmp_func;
376 OPENSSL_sk_cmp_func cmp_func;
377 };
378
sort_compare(void * ctx_v,const void * a,const void * b)379 static int sort_compare(void *ctx_v, const void *a, const void *b) {
380 struct sort_compare_ctx *ctx = ctx_v;
381 return ctx->call_cmp_func(ctx->cmp_func, a, b);
382 }
383 #endif
384
sk_sort(_STACK * sk,OPENSSL_sk_call_cmp_func call_cmp_func)385 void sk_sort(_STACK *sk, OPENSSL_sk_call_cmp_func call_cmp_func) {
386 if (sk == NULL || sk->comp == NULL || sk->sorted) {
387 return;
388 }
389
390 if (sk->num >= 2) {
391 #if defined(_MSC_VER)
392 // MSVC's |qsort_s| is different from the C11 one.
393 // https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/qsort-s?view=msvc-170
394 struct sort_compare_ctx ctx = {call_cmp_func, sk->comp};
395 qsort_s(sk->data, sk->num, sizeof(void *), sort_compare, &ctx);
396 #else
397 // sk->comp is a function that takes pointers to pointers to elements, but
398 // qsort take a comparison function that just takes pointers to elements.
399 // However, since we're passing an array of pointers to qsort, we can just
400 // cast the comparison function and everything works.
401 //
402 // TODO(davidben): This is undefined behavior, but the call is in libc so,
403 // e.g., CFI does not notice. |qsort| is missing a void* parameter in its
404 // callback, while no one defines |qsort_r| or |qsort_s| consistently. See
405 // https://stackoverflow.com/a/39561369
406 int (*comp_func)(const void *, const void *) =
407 (int (*)(const void *, const void *))(sk->comp);
408 qsort(sk->data, sk->num, sizeof(void *), comp_func);
409 #endif
410 }
411 sk->sorted = 1;
412 }
413
sk_is_sorted(const _STACK * sk)414 int sk_is_sorted(const _STACK *sk) {
415 if (!sk) {
416 return 1;
417 }
418 // Zero- and one-element lists are always sorted.
419 return sk->sorted || (sk->comp != NULL && sk->num < 2);
420 }
421
sk_set_cmp_func(_STACK * sk,OPENSSL_sk_cmp_func comp)422 OPENSSL_sk_cmp_func sk_set_cmp_func(_STACK *sk, OPENSSL_sk_cmp_func comp) {
423 OPENSSL_sk_cmp_func old = sk->comp;
424
425 if (sk->comp != comp) {
426 sk->sorted = 0;
427 }
428 sk->comp = comp;
429
430 return old;
431 }
432
sk_deep_copy(const _STACK * sk,OPENSSL_sk_call_copy_func call_copy_func,OPENSSL_sk_copy_func copy_func,OPENSSL_sk_call_free_func call_free_func,OPENSSL_sk_free_func free_func)433 _STACK *sk_deep_copy(const _STACK *sk, OPENSSL_sk_call_copy_func call_copy_func,
434 OPENSSL_sk_copy_func copy_func,
435 OPENSSL_sk_call_free_func call_free_func,
436 OPENSSL_sk_free_func free_func) {
437 _STACK *ret = sk_dup(sk);
438 if (ret == NULL) {
439 return NULL;
440 }
441
442 for (size_t i = 0; i < ret->num; i++) {
443 if (ret->data[i] == NULL) {
444 continue;
445 }
446 ret->data[i] = call_copy_func(copy_func, ret->data[i]);
447 if (ret->data[i] == NULL) {
448 for (size_t j = 0; j < i; j++) {
449 if (ret->data[j] != NULL) {
450 call_free_func(free_func, ret->data[j]);
451 }
452 }
453 sk_free(ret);
454 return NULL;
455 }
456 }
457
458 return ret;
459 }
460