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