Update copyright year
[openssl.git] / crypto / param_build.c
1 /*
2  * Copyright 2019-2022 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          * 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
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 {
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
76 static 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
93 OSSL_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
107 static 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
115 void 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
124 int 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
129 int 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
136 int 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
142 int 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
149 int 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
155 int 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
162 int 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
168 int 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
175 int 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
182 int 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
189 int 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
195 static int push_BN(OSSL_PARAM_BLD *bld, const char *key,
196                    const BIGNUM *bn, size_t sz, int type)
197 {
198     int n, secure = 0;
199     OSSL_PARAM_BLD_DEF *pd;
200
201     if (!ossl_assert(type == OSSL_PARAM_UNSIGNED_INTEGER
202                      || type == OSSL_PARAM_INTEGER))
203         return 0;
204
205     if (bn != NULL) {
206         if (type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(bn)) {
207             ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED,
208                            "Negative big numbers are unsupported for OSSL_PARAM_UNSIGNED_INTEGER");
209             return 0;
210         }
211
212         n = BN_num_bytes(bn);
213         if (n < 0) {
214             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ZERO_LENGTH_NUMBER);
215             return 0;
216         }
217         if (sz < (size_t)n) {
218             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
219             return 0;
220         }
221         if (BN_get_flags(bn, BN_FLG_SECURE) == BN_FLG_SECURE)
222             secure = 1;
223     }
224     pd = param_push(bld, key, sz, sz, type, secure);
225     if (pd == NULL)
226         return 0;
227     pd->bn = bn;
228     return 1;
229 }
230
231 int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key,
232                            const BIGNUM *bn)
233 {
234     if (BN_is_negative(bn))
235         return push_BN(bld, key, bn, bn == NULL ? 0 : BN_num_bytes(bn) + 1,
236                        OSSL_PARAM_INTEGER);
237     return push_BN(bld, key, bn, bn == NULL ? 0 : BN_num_bytes(bn),
238                    OSSL_PARAM_UNSIGNED_INTEGER);
239 }
240
241 int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
242                                const BIGNUM *bn, size_t sz)
243 {
244     if (BN_is_negative(bn))
245         return push_BN(bld, key, bn, bn == NULL ? 0 : BN_num_bytes(bn),
246                        OSSL_PARAM_INTEGER);
247     return push_BN(bld, key, bn, sz, OSSL_PARAM_UNSIGNED_INTEGER);
248 }
249
250 int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
251                                     const char *buf, size_t bsize)
252 {
253     OSSL_PARAM_BLD_DEF *pd;
254     int secure;
255
256     if (bsize == 0) {
257         bsize = strlen(buf);
258     } else if (bsize > INT_MAX) {
259         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
260         return 0;
261     }
262     secure = CRYPTO_secure_allocated(buf);
263     pd = param_push(bld, key, bsize, bsize + 1, OSSL_PARAM_UTF8_STRING, secure);
264     if (pd == NULL)
265         return 0;
266     pd->string = buf;
267     return 1;
268 }
269
270 int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key,
271                                  char *buf, size_t bsize)
272 {
273     OSSL_PARAM_BLD_DEF *pd;
274
275     if (bsize == 0) {
276         bsize = strlen(buf);
277     } else if (bsize > INT_MAX) {
278         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
279         return 0;
280     }
281     pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_UTF8_PTR, 0);
282     if (pd == NULL)
283         return 0;
284     pd->string = buf;
285     return 1;
286 }
287
288 int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD *bld, const char *key,
289                                      const void *buf, size_t bsize)
290 {
291     OSSL_PARAM_BLD_DEF *pd;
292     int secure;
293
294     if (bsize > INT_MAX) {
295         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
296         return 0;
297     }
298     secure = CRYPTO_secure_allocated(buf);
299     pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_OCTET_STRING, secure);
300     if (pd == NULL)
301         return 0;
302     pd->string = buf;
303     return 1;
304 }
305
306 int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key,
307                                   void *buf, size_t bsize)
308 {
309     OSSL_PARAM_BLD_DEF *pd;
310
311     if (bsize > INT_MAX) {
312         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
313         return 0;
314     }
315     pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_OCTET_PTR, 0);
316     if (pd == NULL)
317         return 0;
318     pd->string = buf;
319     return 1;
320 }
321
322 static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param,
323                                      OSSL_PARAM_ALIGNED_BLOCK *blk,
324                                      OSSL_PARAM_ALIGNED_BLOCK *secure)
325 {
326     int i, num = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
327     OSSL_PARAM_BLD_DEF *pd;
328     void *p;
329
330     for (i = 0; i < num; i++) {
331         pd = sk_OSSL_PARAM_BLD_DEF_value(bld->params, i);
332         param[i].key = pd->key;
333         param[i].data_type = pd->type;
334         param[i].data_size = pd->size;
335         param[i].return_size = OSSL_PARAM_UNMODIFIED;
336
337         if (pd->secure) {
338             p = secure;
339             secure += pd->alloc_blocks;
340         } else {
341             p = blk;
342             blk += pd->alloc_blocks;
343         }
344         param[i].data = p;
345         if (pd->bn != NULL) {
346             /* BIGNUM */
347             if (pd->type == OSSL_PARAM_UNSIGNED_INTEGER)
348                 BN_bn2nativepad(pd->bn, (unsigned char *)p, pd->size);
349             else
350                 BN_signed_bn2native(pd->bn, (unsigned char *)p, pd->size);
351         } else if (pd->type == OSSL_PARAM_OCTET_PTR
352                    || pd->type == OSSL_PARAM_UTF8_PTR) {
353             /* PTR */
354             *(const void **)p = pd->string;
355         } else if (pd->type == OSSL_PARAM_OCTET_STRING
356                    || pd->type == OSSL_PARAM_UTF8_STRING) {
357             if (pd->string != NULL)
358                 memcpy(p, pd->string, pd->size);
359             else
360                 memset(p, 0, pd->size);
361             if (pd->type == OSSL_PARAM_UTF8_STRING)
362                 ((char *)p)[pd->size] = '\0';
363         } else {
364             /* Number, but could also be a NULL BIGNUM */
365             if (pd->size > sizeof(pd->num))
366                 memset(p, 0, pd->size);
367             else if (pd->size > 0)
368                 memcpy(p, &pd->num, pd->size);
369         }
370     }
371     param[i] = OSSL_PARAM_construct_end();
372     return param + i;
373 }
374
375 OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld)
376 {
377     OSSL_PARAM_ALIGNED_BLOCK *blk, *s = NULL;
378     OSSL_PARAM *params, *last;
379     const int num = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
380     const size_t p_blks = ossl_param_bytes_to_blocks((1 + num) * sizeof(*params));
381     const size_t total = OSSL_PARAM_ALIGN_SIZE * (p_blks + bld->total_blocks);
382     const size_t ss = OSSL_PARAM_ALIGN_SIZE * bld->secure_blocks;
383
384     if (ss > 0) {
385         s = OPENSSL_secure_malloc(ss);
386         if (s == NULL) {
387             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_SECURE_MALLOC_FAILURE);
388             return NULL;
389         }
390     }
391     params = OPENSSL_malloc(total);
392     if (params == NULL) {
393         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
394         OPENSSL_secure_free(s);
395         return NULL;
396     }
397     blk = p_blks + (OSSL_PARAM_ALIGNED_BLOCK *)(params);
398     last = param_bld_convert(bld, params, blk, s);
399     ossl_param_set_secure_block(last, s, ss);
400
401     /* Reset builder for reuse */
402     bld->total_blocks = 0;
403     bld->secure_blocks = 0;
404     free_all_params(bld);
405     return params;
406 }