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