Coverage Report

Created: 2026-09-12 06:55

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