Coverage Report

Created: 2025-06-13 06:56

/src/openssl/crypto/stack/stack.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include "internal/numbers.h"
13
#include "internal/safe_math.h"
14
#include <openssl/stack.h>
15
#include <errno.h>
16
#include <openssl/e_os2.h>      /* For ossl_inline */
17
18
OSSL_SAFE_MATH_SIGNED(int, int)
19
20
/*
21
 * The initial number of nodes in the array.
22
 */
23
static const int min_nodes = 4;
24
static const int max_nodes = SIZE_MAX / sizeof(void *) < INT_MAX
25
    ? (int)(SIZE_MAX / sizeof(void *)) : INT_MAX;
26
27
struct stack_st {
28
    int num;
29
    const void **data;
30
    int sorted;
31
    int num_alloc;
32
    OPENSSL_sk_compfunc comp;
33
    OPENSSL_sk_freefunc_thunk free_thunk;
34
};
35
36
OPENSSL_sk_compfunc OPENSSL_sk_set_cmp_func(OPENSSL_STACK *sk,
37
                                            OPENSSL_sk_compfunc c)
38
0
{
39
0
    OPENSSL_sk_compfunc old = sk->comp;
40
41
0
    if (sk->comp != c)
42
0
        sk->sorted = 0;
43
0
    sk->comp = c;
44
45
0
    return old;
46
0
}
47
48
OPENSSL_STACK *OPENSSL_sk_dup(const OPENSSL_STACK *sk)
49
2
{
50
2
    OPENSSL_STACK *ret;
51
52
2
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
53
0
        goto err;
54
55
2
    if (sk == NULL) {
56
2
        ret->num = 0;
57
2
        ret->sorted = 0;
58
2
        ret->comp = NULL;
59
2
    } else {
60
        /* direct structure assignment */
61
0
        *ret = *sk;
62
0
    }
63
64
2
    if (sk == NULL || sk->num == 0) {
65
        /* postpone |ret->data| allocation */
66
2
        ret->data = NULL;
67
2
        ret->num_alloc = 0;
68
2
        return ret;
69
2
    }
70
71
    /* duplicate |sk->data| content */
72
0
    ret->data = OPENSSL_malloc(sizeof(*ret->data) * sk->num_alloc);
73
0
    if (ret->data == NULL)
74
0
        goto err;
75
0
    memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
76
0
    return ret;
77
78
0
 err:
79
0
    OPENSSL_sk_free(ret);
80
0
    return NULL;
81
0
}
82
83
OPENSSL_STACK *OPENSSL_sk_deep_copy(const OPENSSL_STACK *sk,
84
                                    OPENSSL_sk_copyfunc copy_func,
85
                                    OPENSSL_sk_freefunc free_func)
86
2
{
87
2
    OPENSSL_STACK *ret;
88
2
    int i;
89
90
2
    if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
91
0
        goto err;
92
93
2
    if (sk == NULL) {
94
2
        ret->num = 0;
95
2
        ret->sorted = 0;
96
2
        ret->comp = NULL;
97
2
    } else {
98
        /* direct structure assignment */
99
0
        *ret = *sk;
100
0
    }
101
102
2
    if (sk == NULL || sk->num == 0) {
103
        /* postpone |ret| data allocation */
104
2
        ret->data = NULL;
105
2
        ret->num_alloc = 0;
106
2
        return ret;
107
2
    }
108
109
0
    ret->num_alloc = sk->num > min_nodes ? sk->num : min_nodes;
110
0
    ret->data = OPENSSL_zalloc(sizeof(*ret->data) * ret->num_alloc);
111
0
    if (ret->data == NULL)
112
0
        goto err;
113
114
0
    for (i = 0; i < ret->num; ++i) {
115
0
        if (sk->data[i] == NULL)
116
0
            continue;
117
0
        if ((ret->data[i] = copy_func(sk->data[i])) == NULL) {
118
0
            while (--i >= 0)
119
0
                if (ret->data[i] != NULL)
120
0
                    free_func((void *)ret->data[i]);
121
0
            goto err;
122
0
        }
123
0
    }
124
0
    return ret;
125
126
0
 err:
127
0
    OPENSSL_sk_free(ret);
128
0
    return NULL;
129
0
}
130
131
OPENSSL_STACK *OPENSSL_sk_new_null(void)
132
20
{
133
20
    return OPENSSL_sk_new_reserve(NULL, 0);
134
20
}
135
136
OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_compfunc c)
137
8
{
138
8
    return OPENSSL_sk_new_reserve(c, 0);
139
8
}
140
141
/*
142
 * Calculate the array growth based on the target size.
143
 *
144
 * The growth factor is a rational number and is defined by a numerator
145
 * and a denominator.  According to Andrew Koenig in his paper "Why Are
146
 * Vectors Efficient?" from JOOP 11(5) 1998, this factor should be less
147
 * than the golden ratio (1.618...).
148
 *
149
 * Considering only the Fibonacci ratios less than the golden ratio, the
150
 * number of steps from the minimum allocation to integer overflow is:
151
 *      factor  decimal    growths
152
 *       3/2     1.5          51
153
 *       8/5     1.6          45
154
 *      21/13    1.615...     44
155
 *
156
 * All larger factors have the same number of growths.
157
 *
158
 * 3/2 and 8/5 have nice power of two shifts, so seem like a good choice.
159
 */
160
static ossl_inline int compute_growth(int target, int current)
161
4
{
162
4
    int err = 0;
163
164
8
    while (current < target) {
165
4
        if (current >= max_nodes)
166
0
            return 0;
167
168
4
        current = safe_muldiv_int(current, 8, 5, &err);
169
4
        if (err != 0)
170
0
            return 0;
171
4
        if (current >= max_nodes)
172
0
            current = max_nodes;
173
4
    }
174
4
    return current;
175
4
}
176
177
/* internal STACK storage allocation */
178
static int sk_reserve(OPENSSL_STACK *st, int n, int exact)
179
36
{
180
36
    const void **tmpdata;
181
36
    int num_alloc;
182
183
    /* Check to see the reservation isn't exceeding the hard limit */
184
36
    if (n > max_nodes - st->num) {
185
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_RECORDS);
186
0
        return 0;
187
0
    }
188
189
    /* Figure out the new size */
190
36
    num_alloc = st->num + n;
191
36
    if (num_alloc < min_nodes)
192
24
        num_alloc = min_nodes;
193
194
    /* If |st->data| allocation was postponed */
195
36
    if (st->data == NULL) {
196
        /*
197
         * At this point, |st->num_alloc| and |st->num| are 0;
198
         * so |num_alloc| value is |n| or |min_nodes| if greater than |n|.
199
         */
200
12
        if ((st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc)) == NULL)
201
0
            return 0;
202
12
        st->num_alloc = num_alloc;
203
12
        return 1;
204
12
    }
205
206
24
    if (!exact) {
207
24
        if (num_alloc <= st->num_alloc)
208
20
            return 1;
209
4
        num_alloc = compute_growth(num_alloc, st->num_alloc);
210
4
        if (num_alloc == 0) {
211
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_RECORDS);
212
0
            return 0;
213
0
        }
214
4
    } else if (num_alloc == st->num_alloc) {
215
0
        return 1;
216
0
    }
217
218
4
    tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);
219
4
    if (tmpdata == NULL)
220
0
        return 0;
221
222
4
    st->data = tmpdata;
223
4
    st->num_alloc = num_alloc;
224
4
    return 1;
225
4
}
226
227
OPENSSL_STACK *OPENSSL_sk_new_reserve(OPENSSL_sk_compfunc c, int n)
228
28
{
229
28
    OPENSSL_STACK *st = OPENSSL_zalloc(sizeof(OPENSSL_STACK));
230
231
28
    if (st == NULL)
232
0
        return NULL;
233
234
28
    st->comp = c;
235
236
28
    if (n <= 0)
237
28
        return st;
238
239
0
    if (!sk_reserve(st, n, 1)) {
240
0
        OPENSSL_sk_free(st);
241
0
        return NULL;
242
0
    }
243
244
0
    return st;
245
0
}
246
247
int OPENSSL_sk_reserve(OPENSSL_STACK *st, int n)
248
0
{
249
0
    if (st == NULL) {
250
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
251
0
        return 0;
252
0
    }
253
254
0
    if (n < 0)
255
0
        return 1;
256
0
    return sk_reserve(st, n, 1);
257
0
}
258
259
OPENSSL_STACK *OPENSSL_sk_set_thunks(OPENSSL_STACK *st, OPENSSL_sk_freefunc_thunk f_thunk)
260
58
{
261
58
    if (st != NULL)
262
12
        st->free_thunk = f_thunk;
263
264
58
    return st;
265
58
}
266
267
int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)
268
36
{
269
36
    if (st == NULL) {
270
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
271
0
        return 0;
272
0
    }
273
36
    if (st->num == max_nodes) {
274
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_RECORDS);
275
0
        return 0;
276
0
    }
277
278
36
    if (!sk_reserve(st, 1, 0))
279
0
        return 0;
280
281
36
    if ((loc >= st->num) || (loc < 0)) {
282
36
        st->data[st->num] = data;
283
36
    } else {
284
0
        memmove(&st->data[loc + 1], &st->data[loc],
285
0
                sizeof(st->data[0]) * (st->num - loc));
286
0
        st->data[loc] = data;
287
0
    }
288
36
    st->num++;
289
36
    st->sorted = 0;
290
36
    return st->num;
291
36
}
292
293
static ossl_inline void *internal_delete(OPENSSL_STACK *st, int loc)
294
2
{
295
2
    const void *ret = st->data[loc];
296
297
2
    if (loc != st->num - 1)
298
0
        memmove(&st->data[loc], &st->data[loc + 1],
299
0
                sizeof(st->data[0]) * (st->num - loc - 1));
300
2
    st->num--;
301
302
2
    return (void *)ret;
303
2
}
304
305
void *OPENSSL_sk_delete_ptr(OPENSSL_STACK *st, const void *p)
306
0
{
307
0
    int i;
308
309
0
    if (st == NULL)
310
0
        return NULL;
311
312
0
    for (i = 0; i < st->num; i++)
313
0
        if (st->data[i] == p)
314
0
            return internal_delete(st, i);
315
0
    return NULL;
316
0
}
317
318
void *OPENSSL_sk_delete(OPENSSL_STACK *st, int loc)
319
2
{
320
2
    if (st == NULL || loc < 0 || loc >= st->num)
321
0
        return NULL;
322
323
2
    return internal_delete(st, loc);
324
2
}
325
326
static int internal_find(OPENSSL_STACK *st, const void *data,
327
                         int ret_val_options, int *pnum_matched)
328
4
{
329
4
    const void *r;
330
4
    int i, count = 0;
331
4
    int *pnum = pnum_matched;
332
333
4
    if (st == NULL || st->num == 0)
334
4
        return -1;
335
336
0
    if (pnum == NULL)
337
0
        pnum = &count;
338
339
0
    if (st->comp == NULL) {
340
0
        for (i = 0; i < st->num; i++)
341
0
            if (st->data[i] == data) {
342
0
                *pnum = 1;
343
0
                return i;
344
0
            }
345
0
        *pnum = 0;
346
0
        return -1;
347
0
    }
348
349
0
    if (data == NULL)
350
0
        return -1;
351
352
0
    if (!st->sorted) {
353
0
        int res = -1;
354
355
0
        for (i = 0; i < st->num; i++)
356
0
            if (st->comp(&data, st->data + i) == 0) {
357
0
                if (res == -1)
358
0
                    res = i;
359
0
                ++*pnum;
360
                /* Check if only one result is wanted and exit if so */
361
0
                if (pnum_matched == NULL)
362
0
                    return i;
363
0
            }
364
0
        if (res == -1)
365
0
            *pnum = 0;
366
0
        return res;
367
0
    }
368
369
0
    if (pnum_matched != NULL)
370
0
        ret_val_options |= OSSL_BSEARCH_FIRST_VALUE_ON_MATCH;
371
0
    r = ossl_bsearch(&data, st->data, st->num, sizeof(void *), st->comp,
372
0
                     ret_val_options);
373
374
0
    if (pnum_matched != NULL) {
375
0
        *pnum = 0;
376
0
        if (r != NULL) {
377
0
            const void **p = (const void **)r;
378
379
0
            while (p < st->data + st->num) {
380
0
                if (st->comp(&data, p) != 0)
381
0
                    break;
382
0
                ++*pnum;
383
0
                ++p;
384
0
            }
385
0
        }
386
0
    }
387
388
0
    return r == NULL ? -1 : (int)((const void **)r - st->data);
389
0
}
390
391
int OPENSSL_sk_find(OPENSSL_STACK *st, const void *data)
392
4
{
393
4
    return internal_find(st, data, OSSL_BSEARCH_FIRST_VALUE_ON_MATCH, NULL);
394
4
}
395
396
int OPENSSL_sk_find_ex(OPENSSL_STACK *st, const void *data)
397
0
{
398
0
    return internal_find(st, data, OSSL_BSEARCH_VALUE_ON_NOMATCH, NULL);
399
0
}
400
401
int OPENSSL_sk_find_all(OPENSSL_STACK *st, const void *data, int *pnum)
402
0
{
403
0
    return internal_find(st, data, OSSL_BSEARCH_FIRST_VALUE_ON_MATCH, pnum);
404
0
}
405
406
int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)
407
36
{
408
36
    if (st == NULL)
409
0
        return 0;
410
36
    return OPENSSL_sk_insert(st, data, st->num);
411
36
}
412
413
int OPENSSL_sk_unshift(OPENSSL_STACK *st, const void *data)
414
0
{
415
0
    return OPENSSL_sk_insert(st, data, 0);
416
0
}
417
418
void *OPENSSL_sk_shift(OPENSSL_STACK *st)
419
0
{
420
0
    if (st == NULL || st->num == 0)
421
0
        return NULL;
422
0
    return internal_delete(st, 0);
423
0
}
424
425
void *OPENSSL_sk_pop(OPENSSL_STACK *st)
426
0
{
427
0
    if (st == NULL || st->num == 0)
428
0
        return NULL;
429
0
    return internal_delete(st, st->num - 1);
430
0
}
431
432
void OPENSSL_sk_zero(OPENSSL_STACK *st)
433
0
{
434
0
    if (st == NULL || st->num == 0)
435
0
        return;
436
0
    memset(st->data, 0, sizeof(*st->data) * st->num);
437
0
    st->num = 0;
438
0
}
439
440
void OPENSSL_sk_pop_free(OPENSSL_STACK *st, OPENSSL_sk_freefunc func)
441
56
{
442
56
    int i;
443
444
56
    if (st == NULL)
445
46
        return;
446
447
12
    for (i = 0; i < st->num; i++) {
448
2
        if (st->data[i] != NULL) {
449
2
            if (st->free_thunk != NULL)
450
2
                st->free_thunk(func, (void *)st->data[i]);
451
0
            else
452
0
                func((void *)st->data[i]);
453
2
        }
454
2
    }
455
10
    OPENSSL_sk_free(st);
456
10
}
457
458
void OPENSSL_sk_free(OPENSSL_STACK *st)
459
28
{
460
28
    if (st == NULL)
461
10
        return;
462
18
    OPENSSL_free(st->data);
463
18
    OPENSSL_free(st);
464
18
}
465
466
int OPENSSL_sk_num(const OPENSSL_STACK *st)
467
14
{
468
14
    return st == NULL ? -1 : st->num;
469
14
}
470
471
void *OPENSSL_sk_value(const OPENSSL_STACK *st, int i)
472
2
{
473
2
    if (st == NULL || i < 0 || i >= st->num)
474
0
        return NULL;
475
2
    return (void *)st->data[i];
476
2
}
477
478
void *OPENSSL_sk_set(OPENSSL_STACK *st, int i, const void *data)
479
0
{
480
0
    if (st == NULL) {
481
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
482
0
        return NULL;
483
0
    }
484
0
    if (i < 0 || i >= st->num) {
485
0
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
486
0
                       "i=%d", i);
487
0
        return NULL;
488
0
    }
489
0
    st->data[i] = data;
490
0
    st->sorted = 0;
491
0
    return (void *)st->data[i];
492
0
}
493
494
void OPENSSL_sk_sort(OPENSSL_STACK *st)
495
2
{
496
2
    if (st != NULL && !st->sorted && st->comp != NULL) {
497
2
        if (st->num > 1)
498
0
            qsort(st->data, st->num, sizeof(void *), st->comp);
499
2
        st->sorted = 1; /* empty or single-element stack is considered sorted */
500
2
    }
501
2
}
502
503
int OPENSSL_sk_is_sorted(const OPENSSL_STACK *st)
504
0
{
505
0
    return st == NULL ? 1 : st->sorted;
506
0
}