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