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