Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl32/crypto/params_dup.c
Line
Count
Source
1
/*
2
 * Copyright 2021-2025 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 <string.h>
11
#include <openssl/params.h>
12
#include <openssl/param_build.h>
13
#include "internal/param_build_set.h"
14
15
610k
#define OSSL_PARAM_ALLOCATED_END    127
16
3.62k
#define OSSL_PARAM_MERGE_LIST_MAX   128
17
18
462
#define OSSL_PARAM_BUF_PUBLIC 0
19
693
#define OSSL_PARAM_BUF_SECURE 1
20
#define OSSL_PARAM_BUF_MAX    (OSSL_PARAM_BUF_SECURE + 1)
21
22
typedef struct {
23
    OSSL_PARAM_ALIGNED_BLOCK *alloc; /* The allocated buffer */
24
    OSSL_PARAM_ALIGNED_BLOCK *cur;   /* Current position in the allocated buf */
25
    size_t blocks;    /* Number of aligned blocks */
26
    size_t alloc_sz;  /* The size of the allocated buffer (in bytes) */
27
} OSSL_PARAM_BUF;
28
29
size_t ossl_param_bytes_to_blocks(size_t bytes)
30
2.38M
{
31
2.38M
    return (bytes + OSSL_PARAM_ALIGN_SIZE - 1) / OSSL_PARAM_ALIGN_SIZE;
32
2.38M
}
33
34
static int ossl_param_buf_alloc(OSSL_PARAM_BUF *out, size_t extra_blocks,
35
                                int is_secure)
36
116
{
37
116
    size_t sz = OSSL_PARAM_ALIGN_SIZE * (extra_blocks + out->blocks);
38
39
116
    out->alloc = is_secure ? OPENSSL_secure_zalloc(sz) : OPENSSL_zalloc(sz);
40
116
    if (out->alloc == NULL)
41
0
        return 0;
42
116
    out->alloc_sz = sz;
43
116
    out->cur = out->alloc + extra_blocks;
44
116
    return 1;
45
116
}
46
47
void ossl_param_set_secure_block(OSSL_PARAM *last, void *secure_buffer,
48
                                 size_t secure_buffer_sz)
49
327k
{
50
327k
    last->key = NULL;
51
327k
    last->data_size = secure_buffer_sz;
52
327k
    last->data = secure_buffer;
53
327k
    last->data_type = OSSL_PARAM_ALLOCATED_END;
54
327k
}
55
56
static OSSL_PARAM *ossl_param_dup(const OSSL_PARAM *src, OSSL_PARAM *dst,
57
                                  OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX],
58
                                  int *param_count)
59
462
{
60
462
    const OSSL_PARAM *in;
61
462
    int has_dst = (dst != NULL);
62
462
    int is_secure;
63
462
    size_t param_sz, blks;
64
65
1.42k
    for (in = src; in->key != NULL; in++) {
66
958
        is_secure = CRYPTO_secure_allocated(in->data);
67
958
        if (has_dst) {
68
479
            *dst = *in;
69
479
            dst->data = buf[is_secure].cur;
70
479
        }
71
72
958
        if (in->data_type == OSSL_PARAM_OCTET_PTR
73
958
            || in->data_type == OSSL_PARAM_UTF8_PTR) {
74
0
            param_sz = sizeof(in->data);
75
0
            if (has_dst)
76
0
                *((const void **)dst->data) = *(const void **)in->data;
77
958
        } else {
78
958
            param_sz = in->data_size;
79
958
            if (has_dst)
80
479
                memcpy(dst->data, in->data, param_sz);
81
958
        }
82
958
        if (in->data_type == OSSL_PARAM_UTF8_STRING)
83
0
            param_sz++; /* NULL terminator */
84
958
        blks = ossl_param_bytes_to_blocks(param_sz);
85
86
958
        if (has_dst) {
87
479
            dst++;
88
479
            buf[is_secure].cur += blks;
89
479
        } else {
90
479
            buf[is_secure].blocks += blks;
91
479
        }
92
958
        if (param_count != NULL)
93
479
            ++*param_count;
94
958
    }
95
462
    return dst;
96
462
}
97
98
OSSL_PARAM *OSSL_PARAM_dup(const OSSL_PARAM *src)
99
231
{
100
231
    size_t param_blocks;
101
231
    OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX];
102
231
    OSSL_PARAM *last, *dst;
103
231
    int param_count = 1; /* Include terminator in the count */
104
105
231
    if (src == NULL) {
106
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
107
0
        return NULL;
108
0
    }
109
110
231
    memset(buf, 0, sizeof(buf));
111
112
    /* First Pass: get the param_count and block sizes required */
113
231
    (void)ossl_param_dup(src, NULL, buf, &param_count);
114
115
231
    param_blocks = ossl_param_bytes_to_blocks(param_count * sizeof(*src));
116
    /*
117
     * The allocated buffer consists of an array of OSSL_PARAM followed by
118
     * aligned data bytes that the array elements will point to.
119
     */
120
231
    if (!ossl_param_buf_alloc(&buf[OSSL_PARAM_BUF_PUBLIC], param_blocks, 0))
121
0
        return NULL;
122
123
    /* Allocate a secure memory buffer if required */
124
231
    if (buf[OSSL_PARAM_BUF_SECURE].blocks > 0
125
0
        && !ossl_param_buf_alloc(&buf[OSSL_PARAM_BUF_SECURE], 0, 1)) {
126
0
        OPENSSL_free(buf[OSSL_PARAM_BUF_PUBLIC].alloc);
127
0
        return NULL;
128
0
    }
129
130
231
    dst = (OSSL_PARAM *)buf[OSSL_PARAM_BUF_PUBLIC].alloc;
131
231
    last = ossl_param_dup(src, dst, buf, NULL);
132
    /* Store the allocated secure memory buffer in the last param block */
133
231
    ossl_param_set_secure_block(last, buf[OSSL_PARAM_BUF_SECURE].alloc,
134
231
                                buf[OSSL_PARAM_BUF_SECURE].alloc_sz);
135
231
    return dst;
136
231
}
137
138
static int compare_params(const void *left, const void *right)
139
0
{
140
0
    const OSSL_PARAM *l = *(const OSSL_PARAM **)left;
141
0
    const OSSL_PARAM *r = *(const OSSL_PARAM **)right;
142
143
0
    return OPENSSL_strcasecmp(l->key, r->key);
144
0
}
145
146
OSSL_PARAM *OSSL_PARAM_merge(const OSSL_PARAM *p1, const OSSL_PARAM *p2)
147
3.62k
{
148
3.62k
    const OSSL_PARAM *list1[OSSL_PARAM_MERGE_LIST_MAX + 1];
149
3.62k
    const OSSL_PARAM *list2[OSSL_PARAM_MERGE_LIST_MAX + 1];
150
3.62k
    const OSSL_PARAM *p = NULL;
151
3.62k
    const OSSL_PARAM **p1cur, **p2cur;
152
3.62k
    OSSL_PARAM *params, *dst;
153
3.62k
    size_t  list1_sz = 0, list2_sz = 0;
154
3.62k
    int diff;
155
156
3.62k
    if (p1 == NULL && p2 == NULL) {
157
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
158
0
        return NULL;
159
0
    }
160
161
    /* Copy p1 to list1 */
162
3.62k
    if (p1 != NULL) {
163
7.25k
        for (p = p1; p->key != NULL && list1_sz < OSSL_PARAM_MERGE_LIST_MAX; p++)
164
3.62k
            list1[list1_sz++] = p;
165
3.62k
    }
166
3.62k
    list1[list1_sz] = NULL;
167
168
    /* copy p2 to a list2 */
169
3.62k
    if (p2 != NULL) {
170
3.62k
        for (p = p2; p->key != NULL && list2_sz < OSSL_PARAM_MERGE_LIST_MAX; p++)
171
0
            list2[list2_sz++] = p;
172
3.62k
    }
173
3.62k
    list2[list2_sz] = NULL;
174
3.62k
    if (list1_sz == 0 && list2_sz == 0) {
175
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_PARAMS_TO_MERGE);
176
0
        return NULL;
177
0
    }
178
179
    /* Sort the 2 lists */
180
3.62k
    qsort(list1, list1_sz, sizeof(OSSL_PARAM *), compare_params);
181
3.62k
    qsort(list2, list2_sz, sizeof(OSSL_PARAM *), compare_params);
182
183
   /* Allocate enough space to store the merged parameters */
184
3.62k
    params = OPENSSL_zalloc((list1_sz + list2_sz + 1) * sizeof(*p1));
185
3.62k
    if (params == NULL)
186
0
        return NULL;
187
3.62k
    dst = params;
188
3.62k
    p1cur = list1;
189
3.62k
    p2cur = list2;
190
3.62k
    while (1) {
191
        /* If list1 is finished just tack list2 onto the end */
192
3.62k
        if (*p1cur == NULL) {
193
0
            while (*p2cur != NULL) {
194
0
                *dst++ = **p2cur;
195
0
                p2cur++;
196
0
            }
197
0
            break;
198
0
        }
199
        /* If list2 is finished just tack list1 onto the end */
200
3.62k
        if (*p2cur == NULL) {
201
7.25k
            while (*p1cur != NULL) {
202
3.62k
                *dst++ = **p1cur;
203
3.62k
                p1cur++;
204
3.62k
            }
205
3.62k
            break;
206
3.62k
        }
207
        /* consume the list element with the smaller key */
208
0
        diff = OPENSSL_strcasecmp((*p1cur)->key, (*p2cur)->key);
209
0
        if (diff == 0) {
210
            /* If the keys are the same then throw away the list1 element */
211
0
            *dst++ = **p2cur;
212
0
            p2cur++;
213
0
            p1cur++;
214
0
        } else if (diff > 0) {
215
0
            *dst++ = **p2cur;
216
0
            p2cur++;
217
0
        } else {
218
0
            *dst++ = **p1cur;
219
0
            p1cur++;
220
0
        }
221
0
    }
222
3.62k
    return params;
223
3.62k
}
224
225
void OSSL_PARAM_free(OSSL_PARAM *params)
226
282k
{
227
282k
    if (params != NULL) {
228
282k
        OSSL_PARAM *p;
229
230
2.12M
        for (p = params; p->key != NULL; p++)
231
1.84M
            ;
232
282k
        if (p->data_type == OSSL_PARAM_ALLOCATED_END)
233
271k
            OPENSSL_secure_clear_free(p->data, p->data_size);
234
282k
        OPENSSL_free(params);
235
282k
    }
236
282k
}