VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.4/crypto/param_build.c@ 103582

最後變更 在這個檔案從103582是 102863,由 vboxsync 提交於 13 月 前

openssl-3.1.4: Applied and adjusted our OpenSSL changes to 3.1.3. bugref:10577

檔案大小: 11.3 KB
 
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
24typedef 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 * imporant 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
43DEFINE_STACK_OF(OSSL_PARAM_BLD_DEF)
44
45struct ossl_param_bld_st {
46 size_t total_blocks;
47 size_t secure_blocks;
48 STACK_OF(OSSL_PARAM_BLD_DEF) *params;
49};
50
51static 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{
55 OSSL_PARAM_BLD_DEF *pd = OPENSSL_zalloc(sizeof(*pd));
56
57 if (pd == NULL) {
58 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
59 return NULL;
60 }
61 pd->key = key;
62 pd->type = type;
63 pd->size = size;
64 pd->alloc_blocks = ossl_param_bytes_to_blocks(alloc);
65 if ((pd->secure = secure) != 0)
66 bld->secure_blocks += pd->alloc_blocks;
67 else
68 bld->total_blocks += pd->alloc_blocks;
69 if (sk_OSSL_PARAM_BLD_DEF_push(bld->params, pd) <= 0) {
70 OPENSSL_free(pd);
71 pd = NULL;
72 }
73 return pd;
74}
75
76static int param_push_num(OSSL_PARAM_BLD *bld, const char *key,
77 void *num, size_t size, int type)
78{
79 OSSL_PARAM_BLD_DEF *pd = param_push(bld, key, size, size, type, 0);
80
81 if (pd == NULL) {
82 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
83 return 0;
84 }
85 if (size > sizeof(pd->num)) {
86 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES);
87 return 0;
88 }
89 memcpy(&pd->num, num, size);
90 return 1;
91}
92
93OSSL_PARAM_BLD *OSSL_PARAM_BLD_new(void)
94{
95 OSSL_PARAM_BLD *r = OPENSSL_zalloc(sizeof(OSSL_PARAM_BLD));
96
97 if (r != NULL) {
98 r->params = sk_OSSL_PARAM_BLD_DEF_new_null();
99 if (r->params == NULL) {
100 OPENSSL_free(r);
101 r = NULL;
102 }
103 }
104 return r;
105}
106
107static void free_all_params(OSSL_PARAM_BLD *bld)
108{
109 int i, n = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
110
111 for (i = 0; i < n; i++)
112 OPENSSL_free(sk_OSSL_PARAM_BLD_DEF_pop(bld->params));
113}
114
115void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD *bld)
116{
117 if (bld == NULL)
118 return;
119 free_all_params(bld);
120 sk_OSSL_PARAM_BLD_DEF_free(bld->params);
121 OPENSSL_free(bld);
122}
123
124int OSSL_PARAM_BLD_push_int(OSSL_PARAM_BLD *bld, const char *key, int num)
125{
126 return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
127}
128
129int OSSL_PARAM_BLD_push_uint(OSSL_PARAM_BLD *bld, const char *key,
130 unsigned int num)
131{
132 return param_push_num(bld, key, &num, sizeof(num),
133 OSSL_PARAM_UNSIGNED_INTEGER);
134}
135
136int OSSL_PARAM_BLD_push_long(OSSL_PARAM_BLD *bld, const char *key,
137 long int num)
138{
139 return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
140}
141
142int OSSL_PARAM_BLD_push_ulong(OSSL_PARAM_BLD *bld, const char *key,
143 unsigned long int num)
144{
145 return param_push_num(bld, key, &num, sizeof(num),
146 OSSL_PARAM_UNSIGNED_INTEGER);
147}
148
149int OSSL_PARAM_BLD_push_int32(OSSL_PARAM_BLD *bld, const char *key,
150 int32_t num)
151{
152 return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
153}
154
155int OSSL_PARAM_BLD_push_uint32(OSSL_PARAM_BLD *bld, const char *key,
156 uint32_t num)
157{
158 return param_push_num(bld, key, &num, sizeof(num),
159 OSSL_PARAM_UNSIGNED_INTEGER);
160}
161
162int OSSL_PARAM_BLD_push_int64(OSSL_PARAM_BLD *bld, const char *key,
163 int64_t num)
164{
165 return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
166}
167
168int OSSL_PARAM_BLD_push_uint64(OSSL_PARAM_BLD *bld, const char *key,
169 uint64_t num)
170{
171 return param_push_num(bld, key, &num, sizeof(num),
172 OSSL_PARAM_UNSIGNED_INTEGER);
173}
174
175int OSSL_PARAM_BLD_push_size_t(OSSL_PARAM_BLD *bld, const char *key,
176 size_t num)
177{
178 return param_push_num(bld, key, &num, sizeof(num),
179 OSSL_PARAM_UNSIGNED_INTEGER);
180}
181
182int OSSL_PARAM_BLD_push_time_t(OSSL_PARAM_BLD *bld, const char *key,
183 time_t num)
184{
185 return param_push_num(bld, key, &num, sizeof(num),
186 OSSL_PARAM_INTEGER);
187}
188
189int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD *bld, const char *key,
190 double num)
191{
192 return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_REAL);
193}
194
195int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key,
196 const BIGNUM *bn)
197{
198 return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn,
199 bn == NULL ? 0 : BN_num_bytes(bn));
200}
201
202int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
203 const BIGNUM *bn, size_t sz)
204{
205 int n, secure = 0;
206 OSSL_PARAM_BLD_DEF *pd;
207
208 if (bn != NULL) {
209 if (BN_is_negative(bn)) {
210 ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
211 "Negative big numbers are unsupported for OSSL_PARAM");
212 return 0;
213 }
214
215 n = BN_num_bytes(bn);
216 if (n < 0) {
217 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ZERO_LENGTH_NUMBER);
218 return 0;
219 }
220 if (sz < (size_t)n) {
221 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
222 return 0;
223 }
224 if (BN_get_flags(bn, BN_FLG_SECURE) == BN_FLG_SECURE)
225 secure = 1;
226
227 /* The BIGNUM is zero, we must transfer at least one byte */
228 if (sz == 0)
229 sz++;
230 }
231 pd = param_push(bld, key, sz, sz, OSSL_PARAM_UNSIGNED_INTEGER, secure);
232 if (pd == NULL)
233 return 0;
234 pd->bn = bn;
235 return 1;
236}
237
238int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
239 const char *buf, size_t bsize)
240{
241 OSSL_PARAM_BLD_DEF *pd;
242 int secure;
243
244 if (bsize == 0) {
245 bsize = strlen(buf);
246 } else if (bsize > INT_MAX) {
247 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
248 return 0;
249 }
250 secure = CRYPTO_secure_allocated(buf);
251 pd = param_push(bld, key, bsize, bsize + 1, OSSL_PARAM_UTF8_STRING, secure);
252 if (pd == NULL)
253 return 0;
254 pd->string = buf;
255 return 1;
256}
257
258int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key,
259 char *buf, size_t bsize)
260{
261 OSSL_PARAM_BLD_DEF *pd;
262
263 if (bsize == 0) {
264 bsize = strlen(buf);
265 } else if (bsize > INT_MAX) {
266 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
267 return 0;
268 }
269 pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_UTF8_PTR, 0);
270 if (pd == NULL)
271 return 0;
272 pd->string = buf;
273 return 1;
274}
275
276int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD *bld, const char *key,
277 const void *buf, size_t bsize)
278{
279 OSSL_PARAM_BLD_DEF *pd;
280 int secure;
281
282 if (bsize > INT_MAX) {
283 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
284 return 0;
285 }
286 secure = CRYPTO_secure_allocated(buf);
287 pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_OCTET_STRING, secure);
288 if (pd == NULL)
289 return 0;
290 pd->string = buf;
291 return 1;
292}
293
294int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key,
295 void *buf, size_t bsize)
296{
297 OSSL_PARAM_BLD_DEF *pd;
298
299 if (bsize > INT_MAX) {
300 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
301 return 0;
302 }
303 pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_OCTET_PTR, 0);
304 if (pd == NULL)
305 return 0;
306 pd->string = buf;
307 return 1;
308}
309
310static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param,
311 OSSL_PARAM_ALIGNED_BLOCK *blk,
312 OSSL_PARAM_ALIGNED_BLOCK *secure)
313{
314 int i, num = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
315 OSSL_PARAM_BLD_DEF *pd;
316 void *p;
317
318 for (i = 0; i < num; i++) {
319 pd = sk_OSSL_PARAM_BLD_DEF_value(bld->params, i);
320 param[i].key = pd->key;
321 param[i].data_type = pd->type;
322 param[i].data_size = pd->size;
323 param[i].return_size = OSSL_PARAM_UNMODIFIED;
324
325 if (pd->secure) {
326 p = secure;
327 secure += pd->alloc_blocks;
328 } else {
329 p = blk;
330 blk += pd->alloc_blocks;
331 }
332 param[i].data = p;
333 if (pd->bn != NULL) {
334 /* BIGNUM */
335 BN_bn2nativepad(pd->bn, (unsigned char *)p, pd->size);
336 } else if (pd->type == OSSL_PARAM_OCTET_PTR
337 || pd->type == OSSL_PARAM_UTF8_PTR) {
338 /* PTR */
339 *(const void **)p = pd->string;
340 } else if (pd->type == OSSL_PARAM_OCTET_STRING
341 || pd->type == OSSL_PARAM_UTF8_STRING) {
342 if (pd->string != NULL)
343 memcpy(p, pd->string, pd->size);
344 else
345 memset(p, 0, pd->size);
346 if (pd->type == OSSL_PARAM_UTF8_STRING)
347 ((char *)p)[pd->size] = '\0';
348 } else {
349 /* Number, but could also be a NULL BIGNUM */
350 if (pd->size > sizeof(pd->num))
351 memset(p, 0, pd->size);
352 else if (pd->size > 0)
353 memcpy(p, &pd->num, pd->size);
354 }
355 }
356 param[i] = OSSL_PARAM_construct_end();
357 return param + i;
358}
359
360OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld)
361{
362 OSSL_PARAM_ALIGNED_BLOCK *blk, *s = NULL;
363 OSSL_PARAM *params, *last;
364 const int num = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
365 const size_t p_blks = ossl_param_bytes_to_blocks((1 + num) * sizeof(*params));
366 const size_t total = OSSL_PARAM_ALIGN_SIZE * (p_blks + bld->total_blocks);
367 const size_t ss = OSSL_PARAM_ALIGN_SIZE * bld->secure_blocks;
368
369 if (ss > 0) {
370 s = OPENSSL_secure_malloc(ss);
371 if (s == NULL) {
372 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_SECURE_MALLOC_FAILURE);
373 return NULL;
374 }
375 }
376 params = OPENSSL_malloc(total);
377 if (params == NULL) {
378 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
379 OPENSSL_secure_free(s);
380 return NULL;
381 }
382 blk = p_blks + (OSSL_PARAM_ALIGNED_BLOCK *)(params);
383 last = param_bld_convert(bld, params, blk, s);
384 ossl_param_set_secure_block(last, s, ss);
385
386 /* Reset builder for reuse */
387 bld->total_blocks = 0;
388 bld->secure_blocks = 0;
389 free_all_params(bld);
390 return params;
391}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette