Coverage Report

Created: 2025-08-28 07:07

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