/src/openssl32/crypto/param_build.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
4 | | * |
5 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | #include <string.h> |
12 | | #include <openssl/err.h> |
13 | | #include <openssl/cryptoerr.h> |
14 | | #include <openssl/params.h> |
15 | | #include <openssl/types.h> |
16 | | #include <openssl/safestack.h> |
17 | | #include "internal/param_build_set.h" |
18 | | |
19 | | /* |
20 | | * Special internal param type to indicate the end of an allocate OSSL_PARAM |
21 | | * array. |
22 | | */ |
23 | | |
24 | | typedef struct { |
25 | | const char *key; |
26 | | int type; |
27 | | int secure; |
28 | | size_t size; |
29 | | size_t alloc_blocks; |
30 | | const BIGNUM *bn; |
31 | | const void *string; |
32 | | union { |
33 | | /* |
34 | | * These fields are never directly addressed, but their sizes are |
35 | | * important so that all native types can be copied here without overrun. |
36 | | */ |
37 | | ossl_intmax_t i; |
38 | | ossl_uintmax_t u; |
39 | | double d; |
40 | | } num; |
41 | | } OSSL_PARAM_BLD_DEF; |
42 | | |
43 | | DEFINE_STACK_OF(OSSL_PARAM_BLD_DEF) |
44 | | |
45 | | struct ossl_param_bld_st { |
46 | | size_t total_blocks; |
47 | | size_t secure_blocks; |
48 | | STACK_OF(OSSL_PARAM_BLD_DEF) *params; |
49 | | }; |
50 | | |
51 | | static OSSL_PARAM_BLD_DEF *param_push(OSSL_PARAM_BLD *bld, const char *key, |
52 | | int size, size_t alloc, int type, |
53 | | int secure) |
54 | 1.11M | { |
55 | 1.11M | OSSL_PARAM_BLD_DEF *pd = OPENSSL_zalloc(sizeof(*pd)); |
56 | | |
57 | 1.11M | if (pd == NULL) |
58 | 0 | return NULL; |
59 | 1.11M | pd->key = key; |
60 | 1.11M | pd->type = type; |
61 | 1.11M | pd->size = size; |
62 | 1.11M | pd->alloc_blocks = ossl_param_bytes_to_blocks(alloc); |
63 | 1.11M | if ((pd->secure = secure) != 0) |
64 | 228k | bld->secure_blocks += pd->alloc_blocks; |
65 | 882k | else |
66 | 882k | bld->total_blocks += pd->alloc_blocks; |
67 | 1.11M | if (sk_OSSL_PARAM_BLD_DEF_push(bld->params, pd) <= 0) { |
68 | 0 | OPENSSL_free(pd); |
69 | 0 | pd = NULL; |
70 | 0 | } |
71 | 1.11M | return pd; |
72 | 1.11M | } |
73 | | |
74 | | static int param_push_num(OSSL_PARAM_BLD *bld, const char *key, |
75 | | void *num, size_t size, int type) |
76 | 254k | { |
77 | 254k | OSSL_PARAM_BLD_DEF *pd = param_push(bld, key, size, size, type, 0); |
78 | | |
79 | 254k | if (pd == NULL) { |
80 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
81 | 0 | return 0; |
82 | 0 | } |
83 | 254k | if (size > sizeof(pd->num)) { |
84 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES); |
85 | 0 | return 0; |
86 | 0 | } |
87 | 254k | memcpy(&pd->num, num, size); |
88 | 254k | return 1; |
89 | 254k | } |
90 | | |
91 | | OSSL_PARAM_BLD *OSSL_PARAM_BLD_new(void) |
92 | 178k | { |
93 | 178k | OSSL_PARAM_BLD *r = OPENSSL_zalloc(sizeof(OSSL_PARAM_BLD)); |
94 | | |
95 | 178k | if (r != NULL) { |
96 | 178k | r->params = sk_OSSL_PARAM_BLD_DEF_new_null(); |
97 | 178k | if (r->params == NULL) { |
98 | 0 | OPENSSL_free(r); |
99 | 0 | r = NULL; |
100 | 0 | } |
101 | 178k | } |
102 | 178k | return r; |
103 | 178k | } |
104 | | |
105 | | static void free_all_params(OSSL_PARAM_BLD *bld) |
106 | 356k | { |
107 | 356k | int i, n = sk_OSSL_PARAM_BLD_DEF_num(bld->params); |
108 | | |
109 | 1.46M | for (i = 0; i < n; i++) |
110 | 1.11M | OPENSSL_free(sk_OSSL_PARAM_BLD_DEF_pop(bld->params)); |
111 | 356k | } |
112 | | |
113 | | void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD *bld) |
114 | 178k | { |
115 | 178k | if (bld == NULL) |
116 | 0 | return; |
117 | 178k | free_all_params(bld); |
118 | 178k | sk_OSSL_PARAM_BLD_DEF_free(bld->params); |
119 | 178k | OPENSSL_free(bld); |
120 | 178k | } |
121 | | |
122 | | int OSSL_PARAM_BLD_push_int(OSSL_PARAM_BLD *bld, const char *key, int num) |
123 | 254k | { |
124 | 254k | return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); |
125 | 254k | } |
126 | | |
127 | | int OSSL_PARAM_BLD_push_uint(OSSL_PARAM_BLD *bld, const char *key, |
128 | | unsigned int num) |
129 | 0 | { |
130 | 0 | return param_push_num(bld, key, &num, sizeof(num), |
131 | 0 | OSSL_PARAM_UNSIGNED_INTEGER); |
132 | 0 | } |
133 | | |
134 | | int OSSL_PARAM_BLD_push_long(OSSL_PARAM_BLD *bld, const char *key, |
135 | | long int num) |
136 | 0 | { |
137 | 0 | return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); |
138 | 0 | } |
139 | | |
140 | | int OSSL_PARAM_BLD_push_ulong(OSSL_PARAM_BLD *bld, const char *key, |
141 | | unsigned long int num) |
142 | 0 | { |
143 | 0 | return param_push_num(bld, key, &num, sizeof(num), |
144 | 0 | OSSL_PARAM_UNSIGNED_INTEGER); |
145 | 0 | } |
146 | | |
147 | | int OSSL_PARAM_BLD_push_int32(OSSL_PARAM_BLD *bld, const char *key, |
148 | | int32_t num) |
149 | 0 | { |
150 | 0 | return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); |
151 | 0 | } |
152 | | |
153 | | int OSSL_PARAM_BLD_push_uint32(OSSL_PARAM_BLD *bld, const char *key, |
154 | | uint32_t num) |
155 | 0 | { |
156 | 0 | return param_push_num(bld, key, &num, sizeof(num), |
157 | 0 | OSSL_PARAM_UNSIGNED_INTEGER); |
158 | 0 | } |
159 | | |
160 | | int OSSL_PARAM_BLD_push_int64(OSSL_PARAM_BLD *bld, const char *key, |
161 | | int64_t num) |
162 | 0 | { |
163 | 0 | return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); |
164 | 0 | } |
165 | | |
166 | | int OSSL_PARAM_BLD_push_uint64(OSSL_PARAM_BLD *bld, const char *key, |
167 | | uint64_t num) |
168 | 0 | { |
169 | 0 | return param_push_num(bld, key, &num, sizeof(num), |
170 | 0 | OSSL_PARAM_UNSIGNED_INTEGER); |
171 | 0 | } |
172 | | |
173 | | int OSSL_PARAM_BLD_push_size_t(OSSL_PARAM_BLD *bld, const char *key, |
174 | | size_t num) |
175 | 0 | { |
176 | 0 | return param_push_num(bld, key, &num, sizeof(num), |
177 | 0 | OSSL_PARAM_UNSIGNED_INTEGER); |
178 | 0 | } |
179 | | |
180 | | int OSSL_PARAM_BLD_push_time_t(OSSL_PARAM_BLD *bld, const char *key, |
181 | | time_t num) |
182 | 0 | { |
183 | 0 | return param_push_num(bld, key, &num, sizeof(num), |
184 | 0 | OSSL_PARAM_INTEGER); |
185 | 0 | } |
186 | | |
187 | | int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD *bld, const char *key, |
188 | | double num) |
189 | 0 | { |
190 | 0 | return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_REAL); |
191 | 0 | } |
192 | | |
193 | | static int push_BN(OSSL_PARAM_BLD *bld, const char *key, |
194 | | const BIGNUM *bn, size_t sz, int type) |
195 | 352k | { |
196 | 352k | int n, secure = 0; |
197 | 352k | OSSL_PARAM_BLD_DEF *pd; |
198 | | |
199 | 352k | if (!ossl_assert(type == OSSL_PARAM_UNSIGNED_INTEGER |
200 | 352k | || type == OSSL_PARAM_INTEGER)) |
201 | 0 | return 0; |
202 | | |
203 | 352k | if (bn != NULL) { |
204 | 352k | if (type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(bn)) { |
205 | 0 | ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED, |
206 | 0 | "Negative big numbers are unsupported for OSSL_PARAM_UNSIGNED_INTEGER"); |
207 | 0 | return 0; |
208 | 0 | } |
209 | | |
210 | 352k | n = BN_num_bytes(bn); |
211 | 352k | if (n < 0) { |
212 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ZERO_LENGTH_NUMBER); |
213 | 0 | return 0; |
214 | 0 | } |
215 | 352k | if (sz < (size_t)n) { |
216 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
217 | 0 | return 0; |
218 | 0 | } |
219 | 352k | if (BN_get_flags(bn, BN_FLG_SECURE) == BN_FLG_SECURE) |
220 | 138k | secure = 1; |
221 | | |
222 | | /* The BIGNUM is zero, we must transfer at least one byte */ |
223 | 352k | if (sz == 0) |
224 | 882 | sz++; |
225 | 352k | } |
226 | 352k | pd = param_push(bld, key, sz, sz, type, secure); |
227 | 352k | if (pd == NULL) |
228 | 0 | return 0; |
229 | 352k | pd->bn = bn; |
230 | 352k | return 1; |
231 | 352k | } |
232 | | |
233 | | int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key, |
234 | | const BIGNUM *bn) |
235 | 321k | { |
236 | 321k | if (bn != NULL && BN_is_negative(bn)) |
237 | 0 | return push_BN(bld, key, bn, BN_num_bytes(bn) + 1, |
238 | 0 | OSSL_PARAM_INTEGER); |
239 | 321k | return push_BN(bld, key, bn, bn == NULL ? 0 : BN_num_bytes(bn), |
240 | 321k | OSSL_PARAM_UNSIGNED_INTEGER); |
241 | 321k | } |
242 | | |
243 | | int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key, |
244 | | const BIGNUM *bn, size_t sz) |
245 | 30.6k | { |
246 | 30.6k | if (bn != NULL && BN_is_negative(bn)) |
247 | 0 | return push_BN(bld, key, bn, BN_num_bytes(bn), |
248 | 0 | OSSL_PARAM_INTEGER); |
249 | 30.6k | return push_BN(bld, key, bn, sz, OSSL_PARAM_UNSIGNED_INTEGER); |
250 | 30.6k | } |
251 | | |
252 | | int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key, |
253 | | const char *buf, size_t bsize) |
254 | 126k | { |
255 | 126k | OSSL_PARAM_BLD_DEF *pd; |
256 | 126k | int secure; |
257 | | |
258 | 126k | if (bsize == 0) |
259 | 126k | bsize = strlen(buf); |
260 | 126k | if (bsize > INT_MAX) { |
261 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); |
262 | 0 | return 0; |
263 | 0 | } |
264 | 126k | secure = CRYPTO_secure_allocated(buf); |
265 | 126k | pd = param_push(bld, key, bsize, bsize + 1, OSSL_PARAM_UTF8_STRING, secure); |
266 | 126k | if (pd == NULL) |
267 | 0 | return 0; |
268 | 126k | pd->string = buf; |
269 | 126k | return 1; |
270 | 126k | } |
271 | | |
272 | | int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key, |
273 | | char *buf, size_t bsize) |
274 | 0 | { |
275 | 0 | OSSL_PARAM_BLD_DEF *pd; |
276 | |
|
277 | 0 | if (bsize == 0) |
278 | 0 | bsize = strlen(buf); |
279 | 0 | if (bsize > INT_MAX) { |
280 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); |
281 | 0 | return 0; |
282 | 0 | } |
283 | 0 | pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_UTF8_PTR, 0); |
284 | 0 | if (pd == NULL) |
285 | 0 | return 0; |
286 | 0 | pd->string = buf; |
287 | 0 | return 1; |
288 | 0 | } |
289 | | |
290 | | int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD *bld, const char *key, |
291 | | const void *buf, size_t bsize) |
292 | 30.9k | { |
293 | 30.9k | OSSL_PARAM_BLD_DEF *pd; |
294 | 30.9k | int secure; |
295 | | |
296 | 30.9k | if (bsize > INT_MAX) { |
297 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); |
298 | 0 | return 0; |
299 | 0 | } |
300 | 30.9k | secure = CRYPTO_secure_allocated(buf); |
301 | 30.9k | pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_OCTET_STRING, secure); |
302 | 30.9k | if (pd == NULL) |
303 | 0 | return 0; |
304 | 30.9k | pd->string = buf; |
305 | 30.9k | return 1; |
306 | 30.9k | } |
307 | | |
308 | | int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key, |
309 | | void *buf, size_t bsize) |
310 | 0 | { |
311 | 0 | OSSL_PARAM_BLD_DEF *pd; |
312 | |
|
313 | 0 | if (bsize > INT_MAX) { |
314 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); |
315 | 0 | return 0; |
316 | 0 | } |
317 | 0 | pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_OCTET_PTR, 0); |
318 | 0 | if (pd == NULL) |
319 | 0 | return 0; |
320 | 0 | pd->string = buf; |
321 | 0 | return 1; |
322 | 0 | } |
323 | | |
324 | | static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param, |
325 | | OSSL_PARAM_ALIGNED_BLOCK *blk, |
326 | | OSSL_PARAM_ALIGNED_BLOCK *secure) |
327 | 123k | { |
328 | 123k | int i, num = sk_OSSL_PARAM_BLD_DEF_num(bld->params); |
329 | 123k | OSSL_PARAM_BLD_DEF *pd; |
330 | 123k | void *p; |
331 | | |
332 | 821k | for (i = 0; i < num; i++) { |
333 | 698k | pd = sk_OSSL_PARAM_BLD_DEF_value(bld->params, i); |
334 | 698k | param[i].key = pd->key; |
335 | 698k | param[i].data_type = pd->type; |
336 | 698k | param[i].data_size = pd->size; |
337 | 698k | param[i].return_size = OSSL_PARAM_UNMODIFIED; |
338 | | |
339 | 698k | if (pd->secure) { |
340 | 138k | p = secure; |
341 | 138k | secure += pd->alloc_blocks; |
342 | 560k | } else { |
343 | 560k | p = blk; |
344 | 560k | blk += pd->alloc_blocks; |
345 | 560k | } |
346 | 698k | param[i].data = p; |
347 | 698k | if (pd->bn != NULL) { |
348 | | /* BIGNUM */ |
349 | 352k | if (pd->type == OSSL_PARAM_UNSIGNED_INTEGER) |
350 | 352k | BN_bn2nativepad(pd->bn, (unsigned char *)p, pd->size); |
351 | 0 | else |
352 | 0 | BN_signed_bn2native(pd->bn, (unsigned char *)p, pd->size); |
353 | 352k | } else if (pd->type == OSSL_PARAM_OCTET_PTR |
354 | 346k | || pd->type == OSSL_PARAM_UTF8_PTR) { |
355 | | /* PTR */ |
356 | 0 | *(const void **)p = pd->string; |
357 | 346k | } else if (pd->type == OSSL_PARAM_OCTET_STRING |
358 | 346k | || pd->type == OSSL_PARAM_UTF8_STRING) { |
359 | 192k | if (pd->string != NULL) |
360 | 192k | memcpy(p, pd->string, pd->size); |
361 | 0 | else |
362 | 0 | memset(p, 0, pd->size); |
363 | 192k | if (pd->type == OSSL_PARAM_UTF8_STRING) |
364 | 125k | ((char *)p)[pd->size] = '\0'; |
365 | 192k | } else { |
366 | | /* Number, but could also be a NULL BIGNUM */ |
367 | 153k | if (pd->size > sizeof(pd->num)) |
368 | 0 | memset(p, 0, pd->size); |
369 | 153k | else if (pd->size > 0) |
370 | 153k | memcpy(p, &pd->num, pd->size); |
371 | 153k | } |
372 | 698k | } |
373 | 123k | param[i] = OSSL_PARAM_construct_end(); |
374 | 123k | return param + i; |
375 | 123k | } |
376 | | |
377 | | OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld) |
378 | 178k | { |
379 | 178k | OSSL_PARAM_ALIGNED_BLOCK *blk, *s = NULL; |
380 | 178k | OSSL_PARAM *params, *last; |
381 | 178k | const int num = sk_OSSL_PARAM_BLD_DEF_num(bld->params); |
382 | 178k | const size_t p_blks = ossl_param_bytes_to_blocks((1 + num) * sizeof(*params)); |
383 | 178k | const size_t total = OSSL_PARAM_ALIGN_SIZE * (p_blks + bld->total_blocks); |
384 | 178k | const size_t ss = OSSL_PARAM_ALIGN_SIZE * bld->secure_blocks; |
385 | | |
386 | 178k | if (ss > 0) { |
387 | 101k | s = OPENSSL_secure_malloc(ss); |
388 | 101k | if (s == NULL) { |
389 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_SECURE_MALLOC_FAILURE); |
390 | 0 | return NULL; |
391 | 0 | } |
392 | 101k | } |
393 | 178k | params = OPENSSL_malloc(total); |
394 | 178k | if (params == NULL) { |
395 | 0 | OPENSSL_secure_free(s); |
396 | 0 | return NULL; |
397 | 0 | } |
398 | 178k | blk = p_blks + (OSSL_PARAM_ALIGNED_BLOCK *)(params); |
399 | 178k | last = param_bld_convert(bld, params, blk, s); |
400 | 178k | ossl_param_set_secure_block(last, s, ss); |
401 | | |
402 | | /* Reset builder for reuse */ |
403 | 178k | bld->total_blocks = 0; |
404 | 178k | bld->secure_blocks = 0; |
405 | 178k | free_all_params(bld); |
406 | 178k | return params; |
407 | 178k | } |