Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/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
705k
#define OSSL_PARAM_ALLOCATED_END 127
16
3.24k
#define OSSL_PARAM_MERGE_LIST_MAX 128
17
18
622
#define OSSL_PARAM_BUF_PUBLIC 0
19
933
#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.55M
{
31
2.55M
    return (bytes + OSSL_PARAM_ALIGN_SIZE - 1) / OSSL_PARAM_ALIGN_SIZE;
32
2.55M
}
33
34
static int ossl_param_buf_alloc(OSSL_PARAM_BUF *out, size_t extra_blocks,
35
    int is_secure)
36
{
37
    size_t sz = OSSL_PARAM_ALIGN_SIZE * (extra_blocks + out->blocks);
38
39
    out->alloc = is_secure ? OPENSSL_secure_zalloc(sz) : OPENSSL_zalloc(sz);
40
    if (out->alloc == NULL) {
41
        ERR_raise(ERR_LIB_CRYPTO, is_secure ? CRYPTO_R_SECURE_MALLOC_FAILURE : ERR_R_MALLOC_FAILURE);
42
        return 0;
43
    }
44
    out->alloc_sz = sz;
45
    out->cur = out->alloc + extra_blocks;
46
    return 1;
47
}
48
49
void ossl_param_set_secure_block(OSSL_PARAM *last, void *secure_buffer,
50
    size_t secure_buffer_sz)
51
374k
{
52
374k
    last->key = NULL;
53
374k
    last->data_size = secure_buffer_sz;
54
374k
    last->data = secure_buffer;
55
374k
    last->data_type = OSSL_PARAM_ALLOCATED_END;
56
374k
}
57
58
static OSSL_PARAM *ossl_param_dup(const OSSL_PARAM *src, OSSL_PARAM *dst,
59
    OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX],
60
    int *param_count)
61
622
{
62
622
    const OSSL_PARAM *in;
63
622
    int has_dst = (dst != NULL);
64
622
    int is_secure;
65
622
    size_t param_sz, blks;
66
67
1.92k
    for (in = src; in->key != NULL; in++) {
68
1.30k
        is_secure = CRYPTO_secure_allocated(in->data);
69
1.30k
        if (has_dst) {
70
653
            *dst = *in;
71
653
            dst->data = buf[is_secure].cur;
72
653
        }
73
74
1.30k
        if (in->data_type == OSSL_PARAM_OCTET_PTR
75
1.30k
            || in->data_type == OSSL_PARAM_UTF8_PTR) {
76
0
            param_sz = sizeof(in->data);
77
0
            if (has_dst)
78
0
                *((const void **)dst->data) = *(const void **)in->data;
79
1.30k
        } else {
80
1.30k
            param_sz = in->data_size;
81
1.30k
            if (has_dst)
82
653
                memcpy(dst->data, in->data, param_sz);
83
1.30k
        }
84
1.30k
        if (in->data_type == OSSL_PARAM_UTF8_STRING)
85
0
            param_sz++; /* NULL terminator */
86
1.30k
        blks = ossl_param_bytes_to_blocks(param_sz);
87
88
1.30k
        if (has_dst) {
89
653
            dst++;
90
653
            buf[is_secure].cur += blks;
91
653
        } else {
92
653
            buf[is_secure].blocks += blks;
93
653
        }
94
1.30k
        if (param_count != NULL)
95
653
            ++*param_count;
96
1.30k
    }
97
622
    return dst;
98
622
}
99
100
OSSL_PARAM *OSSL_PARAM_dup(const OSSL_PARAM *src)
101
311
{
102
311
    size_t param_blocks;
103
311
    OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX];
104
311
    OSSL_PARAM *last, *dst;
105
311
    int param_count = 1; /* Include terminator in the count */
106
107
311
    if (src == NULL)
108
0
        return NULL;
109
110
311
    memset(buf, 0, sizeof(buf));
111
112
    /* First Pass: get the param_count and block sizes required */
113
311
    (void)ossl_param_dup(src, NULL, buf, &param_count);
114
115
311
    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
311
    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
311
    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
311
    dst = (OSSL_PARAM *)buf[OSSL_PARAM_BUF_PUBLIC].alloc;
131
311
    last = ossl_param_dup(src, dst, buf, NULL);
132
    /* Store the allocated secure memory buffer in the last param block */
133
311
    ossl_param_set_secure_block(last, buf[OSSL_PARAM_BUF_SECURE].alloc,
134
311
        buf[OSSL_PARAM_BUF_SECURE].alloc_sz);
135
311
    return dst;
136
311
}
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.24k
{
148
3.24k
    const OSSL_PARAM *list1[OSSL_PARAM_MERGE_LIST_MAX + 1];
149
3.24k
    const OSSL_PARAM *list2[OSSL_PARAM_MERGE_LIST_MAX + 1];
150
3.24k
    const OSSL_PARAM *p = NULL;
151
3.24k
    const OSSL_PARAM **p1cur, **p2cur;
152
3.24k
    OSSL_PARAM *params, *dst;
153
3.24k
    size_t list1_sz = 0, list2_sz = 0;
154
3.24k
    int diff;
155
156
3.24k
    if (p1 == NULL && p2 == NULL)
157
0
        return NULL;
158
159
    /* Copy p1 to list1 */
160
3.24k
    if (p1 != NULL) {
161
6.49k
        for (p = p1; p->key != NULL && list1_sz < OSSL_PARAM_MERGE_LIST_MAX; p++)
162
3.24k
            list1[list1_sz++] = p;
163
3.24k
    }
164
3.24k
    list1[list1_sz] = NULL;
165
166
    /* copy p2 to a list2 */
167
3.24k
    if (p2 != NULL) {
168
3.24k
        for (p = p2; p->key != NULL && list2_sz < OSSL_PARAM_MERGE_LIST_MAX; p++)
169
0
            list2[list2_sz++] = p;
170
3.24k
    }
171
3.24k
    list2[list2_sz] = NULL;
172
3.24k
    if (list1_sz == 0 && list2_sz == 0)
173
0
        return NULL;
174
175
    /* Sort the 2 lists */
176
3.24k
    qsort(list1, list1_sz, sizeof(OSSL_PARAM *), compare_params);
177
3.24k
    qsort(list2, list2_sz, sizeof(OSSL_PARAM *), compare_params);
178
179
    /* Allocate enough space to store the merged parameters */
180
3.24k
    params = OPENSSL_zalloc((list1_sz + list2_sz + 1) * sizeof(*p1));
181
3.24k
    if (params == NULL) {
182
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
183
0
        return NULL;
184
0
    }
185
3.24k
    dst = params;
186
3.24k
    p1cur = list1;
187
3.24k
    p2cur = list2;
188
3.24k
    while (1) {
189
        /* If list1 is finished just tack list2 onto the end */
190
3.24k
        if (*p1cur == NULL) {
191
0
            while (*p2cur != NULL) {
192
0
                *dst++ = **p2cur;
193
0
                p2cur++;
194
0
            }
195
0
            break;
196
0
        }
197
        /* If list2 is finished just tack list1 onto the end */
198
3.24k
        if (*p2cur == NULL) {
199
6.49k
            while (*p1cur != NULL) {
200
3.24k
                *dst++ = **p1cur;
201
3.24k
                p1cur++;
202
3.24k
            }
203
3.24k
            break;
204
3.24k
        }
205
        /* consume the list element with the smaller key */
206
0
        diff = OPENSSL_strcasecmp((*p1cur)->key, (*p2cur)->key);
207
0
        if (diff == 0) {
208
            /* If the keys are the same then throw away the list1 element */
209
0
            *dst++ = **p2cur;
210
0
            p2cur++;
211
0
            p1cur++;
212
0
        } else if (diff > 0) {
213
0
            *dst++ = **p2cur;
214
0
            p2cur++;
215
0
        } else {
216
0
            *dst++ = **p1cur;
217
0
            p1cur++;
218
0
        }
219
0
    }
220
3.24k
    return params;
221
3.24k
}
222
223
void OSSL_PARAM_free(OSSL_PARAM *params)
224
331k
{
225
331k
    if (params != NULL) {
226
331k
        OSSL_PARAM *p;
227
228
2.30M
        for (p = params; p->key != NULL; p++)
229
1.97M
            ;
230
331k
        if (p->data_type == OSSL_PARAM_ALLOCATED_END)
231
318k
            OPENSSL_secure_clear_free(p->data, p->data_size);
232
331k
        OPENSSL_free(params);
233
331k
    }
234
331k
}