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