Test that NULL BIGNUM is supported in OSSL_PARAM_BLD_push_BN()
[openssl.git] / test / param_build_test.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/params.h>
13 #include <openssl/param_build.h>
14 #include "internal/nelem.h"
15 #include "testutil.h"
16
17 static const OSSL_PARAM params_empty[] = { OSSL_PARAM_END };
18
19 static int template_public_single_zero_test(int idx)
20 {
21     OSSL_PARAM_BLD *bld = NULL;
22     OSSL_PARAM *params = NULL, *params_blt = NULL, *p;
23     BIGNUM *zbn = NULL, *zbn_res = NULL;
24     int res = 0;
25
26     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
27         || !TEST_ptr(zbn = BN_new())
28         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber",
29                                              idx == 0 ? zbn : NULL))
30         || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
31         goto err;
32
33     params = params_blt;
34     /* Check BN (zero BN becomes unsigned integer) */
35     if (!TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber"))
36         || !TEST_str_eq(p->key, "zeronumber")
37         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
38         || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res))
39         || !TEST_BN_eq(zbn_res, zbn))
40         goto err;
41     res = 1;
42 err:
43     if (params != params_blt)
44         OPENSSL_free(params);
45     OSSL_PARAM_free(params_blt);
46     OSSL_PARAM_BLD_free(bld);
47     BN_free(zbn);
48     BN_free(zbn_res);
49     return res;
50 }
51
52 static int template_private_single_zero_test(void)
53 {
54     OSSL_PARAM_BLD *bld = NULL;
55     OSSL_PARAM *params = NULL, *params_blt = NULL, *p;
56     BIGNUM *zbn = NULL, *zbn_res = NULL;
57     int res = 0;
58
59     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
60         || !TEST_ptr(zbn = BN_secure_new())
61         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn))
62         || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
63         goto err;
64
65     params = params_blt;
66     /* Check BN (zero BN becomes unsigned integer) */
67     if (!TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber"))
68         || !TEST_true(CRYPTO_secure_allocated(p->data))
69         || !TEST_str_eq(p->key, "zeronumber")
70         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
71         || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res))
72         || !TEST_int_eq(BN_get_flags(zbn, BN_FLG_SECURE), BN_FLG_SECURE)
73         || !TEST_BN_eq(zbn_res, zbn))
74         goto err;
75     res = 1;
76 err:
77     if (params != params_blt)
78         OPENSSL_free(params);
79     OSSL_PARAM_free(params_blt);
80     OSSL_PARAM_BLD_free(bld);
81     BN_free(zbn);
82     BN_free(zbn_res);
83     return res;
84 }
85
86 static int template_public_test(int tstid)
87 {
88     OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
89     OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p;
90     BIGNUM *zbn = NULL, *zbn_res = NULL;
91     BIGNUM *pbn = NULL, *pbn_res = NULL;
92     BIGNUM *nbn = NULL, *nbn_res = NULL;
93     int i;
94     long int l;
95     int32_t i32;
96     int64_t i64;
97     double d;
98     time_t t;
99     char *utf = NULL;
100     const char *cutf;
101     int res = 0;
102
103     if (!TEST_ptr(bld)
104         || !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42))
105         || !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532))
106         || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999))
107         || !TEST_true(OSSL_PARAM_BLD_push_time_t(bld, "t", 11224))
108         || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875))
109         || !TEST_ptr(zbn = BN_new())
110         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn))
111         || !TEST_ptr(pbn = BN_new())
112         || !TEST_true(BN_set_word(pbn, 1729))
113         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn))
114         || !TEST_ptr(nbn = BN_secure_new())
115         || !TEST_true(BN_set_word(nbn, 1733))
116         || !TEST_true((BN_set_negative(nbn, 1), 1))
117         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "negativebignumber", nbn))
118         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "utf8_s", "foo",
119                                                       sizeof("foo")))
120         || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom",
121                                                    0))
122         || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6))
123         || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
124         goto err;
125
126     switch (tstid) {
127     case 0:
128         params = params_blt;
129         break;
130     case 1:
131         params = OSSL_PARAM_merge(params_blt, params_empty);
132         break;
133     case 2:
134         params = OSSL_PARAM_dup(params_blt);
135         break;
136     case 3:
137         p1 = OSSL_PARAM_merge(params_blt, params_empty);
138         params = OSSL_PARAM_dup(p1);
139         break;
140     default:
141         p1 = OSSL_PARAM_dup(params_blt);
142         params = OSSL_PARAM_merge(p1, params_empty);
143         break;
144     }
145     /* Check int */
146     if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
147         || !TEST_true(OSSL_PARAM_get_int(p, &i))
148         || !TEST_str_eq(p->key, "i")
149         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
150         || !TEST_size_t_eq(p->data_size, sizeof(int))
151         || !TEST_int_eq(i, -6)
152         /* Check int32 */
153         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
154         || !TEST_true(OSSL_PARAM_get_int32(p, &i32))
155         || !TEST_str_eq(p->key, "i32")
156         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
157         || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
158         || !TEST_int_eq((int)i32, 1532)
159         /* Check int64 */
160         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
161         || !TEST_str_eq(p->key, "i64")
162         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
163         || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
164         || !TEST_true(OSSL_PARAM_get_int64(p, &i64))
165         || !TEST_long_eq((long)i64, -9999999)
166         /* Check long */
167         || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
168         || !TEST_str_eq(p->key, "l")
169         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
170         || !TEST_size_t_eq(p->data_size, sizeof(long int))
171         || !TEST_true(OSSL_PARAM_get_long(p, &l))
172         || !TEST_long_eq(l, 42)
173         /* Check time_t */
174         || !TEST_ptr(p = OSSL_PARAM_locate(params, "t"))
175         || !TEST_str_eq(p->key, "t")
176         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
177         || !TEST_size_t_eq(p->data_size, sizeof(time_t))
178         || !TEST_true(OSSL_PARAM_get_time_t(p, &t))
179         || !TEST_time_t_eq(t, 11224)
180         /* Check double */
181         || !TEST_ptr(p = OSSL_PARAM_locate(params, "d"))
182         || !TEST_true(OSSL_PARAM_get_double(p, &d))
183         || !TEST_str_eq(p->key, "d")
184         || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL)
185         || !TEST_size_t_eq(p->data_size, sizeof(double))
186         || !TEST_double_eq(d, 1.61803398875)
187         /* Check UTF8 string */
188         || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s"))
189         || !TEST_str_eq(p->data, "foo")
190         || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0))
191         || !TEST_str_eq(utf, "foo")
192         /* Check UTF8 pointer */
193         || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
194         || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
195         || !TEST_str_eq(cutf, "bar-boom")
196         /* Check BN (zero BN becomes unsigned integer) */
197         || !TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber"))
198         || !TEST_str_eq(p->key, "zeronumber")
199         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
200         || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res))
201         || !TEST_BN_eq(zbn_res, zbn)
202         /* Check BN (positive BN becomes unsigned integer) */
203         || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
204         || !TEST_str_eq(p->key, "bignumber")
205         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
206         || !TEST_true(OSSL_PARAM_get_BN(p, &pbn_res))
207         || !TEST_BN_eq(pbn_res, pbn)
208         /* Check BN (negative BN becomes signed integer) */
209         || !TEST_ptr(p = OSSL_PARAM_locate(params, "negativebignumber"))
210         || !TEST_str_eq(p->key, "negativebignumber")
211         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
212         || !TEST_true(OSSL_PARAM_get_BN(p, &nbn_res))
213         || !TEST_BN_eq(nbn_res, nbn))
214         goto err;
215     res = 1;
216 err:
217     OPENSSL_free(p1);
218     if (params != params_blt)
219         OPENSSL_free(params);
220     OSSL_PARAM_free(params_blt);
221     OSSL_PARAM_BLD_free(bld);
222     OPENSSL_free(utf);
223     BN_free(zbn);
224     BN_free(zbn_res);
225     BN_free(pbn);
226     BN_free(pbn_res);
227     BN_free(nbn);
228     BN_free(nbn_res);
229     return res;
230 }
231
232 static int template_private_test(int tstid)
233 {
234     int *data1 = NULL, *data2 = NULL, j;
235     const int data1_num = 12;
236     const int data1_size = data1_num * sizeof(int);
237     const int data2_num = 5;
238     const int data2_size = data2_num * sizeof(int);
239     OSSL_PARAM_BLD *bld = NULL;
240     OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p;
241     unsigned int i;
242     unsigned long int l;
243     uint32_t i32;
244     uint64_t i64;
245     size_t st;
246     BIGNUM *zbn = NULL, *zbn_res = NULL;
247     BIGNUM *pbn = NULL, *pbn_res = NULL;
248     BIGNUM *nbn = NULL, *nbn_res = NULL;
249     int res = 0;
250
251     if (!TEST_ptr(data1 = OPENSSL_secure_malloc(data1_size))
252             || !TEST_ptr(data2 = OPENSSL_secure_malloc(data2_size))
253             || !TEST_ptr(bld = OSSL_PARAM_BLD_new()))
254         goto err;
255
256     for (j = 0; j < data1_num; j++)
257         data1[j] = -16 * j;
258     for (j = 0; j < data2_num; j++)
259         data2[j] = 2 * j;
260
261     if (!TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6))
262         || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42))
263         || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
264         || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
265         || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
266         || !TEST_ptr(zbn = BN_secure_new())
267         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn))
268         || !TEST_ptr(pbn = BN_secure_new())
269         || !TEST_true(BN_set_word(pbn, 1729))
270         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn))
271         || !TEST_ptr(nbn = BN_secure_new())
272         || !TEST_true(BN_set_word(nbn, 1733))
273         || !TEST_true((BN_set_negative(nbn, 1), 1))
274         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "negativebignumber", nbn))
275         || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "oct_s", data1,
276                                                        data1_size))
277         || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2,
278                                                     data2_size))
279         || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
280         goto err;
281     switch (tstid) {
282     case 0:
283         params = params_blt;
284         break;
285     case 1:
286         params = OSSL_PARAM_merge(params_blt, params_empty);
287         break;
288     case 2:
289         params = OSSL_PARAM_dup(params_blt);
290         break;
291     case 3:
292         p1 = OSSL_PARAM_merge(params_blt, params_empty);
293         params = OSSL_PARAM_dup(p1);
294         break;
295     default:
296         p1 = OSSL_PARAM_dup(params_blt);
297         params = OSSL_PARAM_merge(p1, params_empty);
298         break;
299     }
300     /* Check unsigned int */
301     if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
302         || !TEST_false(CRYPTO_secure_allocated(p->data))
303         || !TEST_true(OSSL_PARAM_get_uint(p, &i))
304         || !TEST_str_eq(p->key, "i")
305         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
306         || !TEST_size_t_eq(p->data_size, sizeof(int))
307         || !TEST_uint_eq(i, 6)
308         /* Check unsigned int32 */
309         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
310         || !TEST_false(CRYPTO_secure_allocated(p->data))
311         || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
312         || !TEST_str_eq(p->key, "i32")
313         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
314         || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
315         || !TEST_uint_eq((unsigned int)i32, 1532)
316         /* Check unsigned int64 */
317         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
318         || !TEST_false(CRYPTO_secure_allocated(p->data))
319         || !TEST_str_eq(p->key, "i64")
320         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
321         || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
322         || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
323         || !TEST_ulong_eq((unsigned long)i64, 9999999)
324         /* Check unsigned long int */
325         || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
326         || !TEST_false(CRYPTO_secure_allocated(p->data))
327         || !TEST_str_eq(p->key, "l")
328         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
329         || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
330         || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
331         || !TEST_ulong_eq(l, 42)
332         /* Check size_t */
333         || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
334         || !TEST_false(CRYPTO_secure_allocated(p->data))
335         || !TEST_str_eq(p->key, "st")
336         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
337         || !TEST_size_t_eq(p->data_size, sizeof(size_t))
338         || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
339         || !TEST_size_t_eq(st, 65537)
340         /* Check octet string */
341         || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
342         || !TEST_true(CRYPTO_secure_allocated(p->data))
343         || !TEST_str_eq(p->key, "oct_s")
344         || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
345         || !TEST_mem_eq(p->data, p->data_size, data1, data1_size)
346         /* Check octet pointer */
347         || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
348         || !TEST_false(CRYPTO_secure_allocated(p->data))
349         || !TEST_true(CRYPTO_secure_allocated(*(void **)p->data))
350         || !TEST_str_eq(p->key, "oct_p")
351         || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
352         || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, data2_size)
353         /* Check BN (zero BN becomes unsigned integer) */
354         || !TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber"))
355         || !TEST_true(CRYPTO_secure_allocated(p->data))
356         || !TEST_str_eq(p->key, "zeronumber")
357         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
358         || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res))
359         || !TEST_int_eq(BN_get_flags(pbn, BN_FLG_SECURE), BN_FLG_SECURE)
360         || !TEST_BN_eq(zbn_res, zbn)
361         /* Check BN (positive BN becomes unsigned integer) */
362         || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
363         || !TEST_true(CRYPTO_secure_allocated(p->data))
364         || !TEST_str_eq(p->key, "bignumber")
365         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
366         || !TEST_true(OSSL_PARAM_get_BN(p, &pbn_res))
367         || !TEST_int_eq(BN_get_flags(pbn, BN_FLG_SECURE), BN_FLG_SECURE)
368         || !TEST_BN_eq(pbn_res, pbn)
369         /* Check BN (negative BN becomes signed integer) */
370         || !TEST_ptr(p = OSSL_PARAM_locate(params, "negativebignumber"))
371         || !TEST_true(CRYPTO_secure_allocated(p->data))
372         || !TEST_str_eq(p->key, "negativebignumber")
373         || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
374         || !TEST_true(OSSL_PARAM_get_BN(p, &nbn_res))
375         || !TEST_int_eq(BN_get_flags(nbn, BN_FLG_SECURE), BN_FLG_SECURE)
376         || !TEST_BN_eq(nbn_res, nbn))
377         goto err;
378     res = 1;
379 err:
380     OSSL_PARAM_free(p1);
381     if (params != params_blt)
382         OSSL_PARAM_free(params);
383     OSSL_PARAM_free(params_blt);
384     OSSL_PARAM_BLD_free(bld);
385     OPENSSL_secure_free(data1);
386     OPENSSL_secure_free(data2);
387     BN_free(zbn);
388     BN_free(zbn_res);
389     BN_free(pbn);
390     BN_free(pbn_res);
391     BN_free(nbn);
392     BN_free(nbn_res);
393     return res;
394 }
395
396 static int builder_limit_test(void)
397 {
398     const int n = 100;
399     char names[100][3];
400     OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
401     OSSL_PARAM *params = NULL;
402     int i, res = 0;
403
404     if (!TEST_ptr(bld))
405         goto err;
406
407     for (i = 0; i < n; i++) {
408         names[i][0] = 'A' + (i / 26) - 1;
409         names[i][1] = 'a' + (i % 26) - 1;
410         names[i][2] = '\0';
411         if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, names[i], 3 * i + 1)))
412             goto err;
413     }
414     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
415         goto err;
416     /* Count the elements in the params array, expecting n */
417     for (i = 0; params[i].key != NULL; i++);
418     if (!TEST_int_eq(i, n))
419         goto err;
420
421     /* Verify that the build, cleared the builder structure */
422     OSSL_PARAM_free(params);
423     params = NULL;
424
425     if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2))
426         || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
427         goto err;
428     /* Count the elements in the params array, expecting 1 */
429     for (i = 0; params[i].key != NULL; i++);
430     if (!TEST_int_eq(i, 1))
431         goto err;
432     res = 1;
433 err:
434     OSSL_PARAM_free(params);
435     OSSL_PARAM_BLD_free(bld);
436     return res;
437 }
438
439 static int builder_merge_test(void)
440 {
441     static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
442     static unsigned char data2[] = { 2, 4, 6, 8, 10 };
443     OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
444     OSSL_PARAM_BLD *bld2 = OSSL_PARAM_BLD_new();
445     OSSL_PARAM *params = NULL, *params_blt = NULL, *params2_blt = NULL, *p;
446     unsigned int i;
447     unsigned long int l;
448     uint32_t i32;
449     uint64_t i64;
450     size_t st;
451     BIGNUM *bn_priv = NULL, *bn_priv_res = NULL;
452     BIGNUM *bn_pub = NULL, *bn_pub_res = NULL;
453     int res = 0;
454
455     if (!TEST_ptr(bld)
456         || !TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6))
457         || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42))
458         || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
459         || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
460         || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
461         || !TEST_ptr(bn_priv = BN_secure_new())
462         || !TEST_true(BN_set_word(bn_priv, 1729))
463         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber_priv", bn_priv))
464         || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
465         goto err;
466
467     if (!TEST_ptr(bld2)
468         || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld2, "oct_s", data1,
469                                                        sizeof(data1)))
470         || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld2, "oct_p", data2,
471                                                     sizeof(data2)))
472         || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld2, "i32", 99))
473         || !TEST_ptr(bn_pub = BN_new())
474         || !TEST_true(BN_set_word(bn_pub, 0x42))
475         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld2, "bignumber_pub", bn_pub))
476         || !TEST_ptr(params2_blt = OSSL_PARAM_BLD_to_param(bld2)))
477         goto err;
478
479     if (!TEST_ptr(params = OSSL_PARAM_merge(params_blt, params2_blt)))
480         goto err;
481
482     if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
483         || !TEST_true(OSSL_PARAM_get_uint(p, &i))
484         || !TEST_str_eq(p->key, "i")
485         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
486         || !TEST_size_t_eq(p->data_size, sizeof(int))
487         || !TEST_uint_eq(i, 6)
488         /* Check unsigned int32 */
489         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
490         || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
491         || !TEST_str_eq(p->key, "i32")
492         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
493         || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
494         || !TEST_uint_eq((unsigned int)i32, 99)
495         /* Check unsigned int64 */
496         || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
497         || !TEST_str_eq(p->key, "i64")
498         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
499         || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
500         || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
501         || !TEST_ulong_eq((unsigned long)i64, 9999999)
502         /* Check unsigned long int */
503         || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
504         || !TEST_str_eq(p->key, "l")
505         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
506         || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
507         || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
508         || !TEST_ulong_eq(l, 42)
509         /* Check size_t */
510         || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
511         || !TEST_str_eq(p->key, "st")
512         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
513         || !TEST_size_t_eq(p->data_size, sizeof(size_t))
514         || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
515         || !TEST_size_t_eq(st, 65537)
516         /* Check octet string */
517         || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
518         || !TEST_str_eq(p->key, "oct_s")
519         || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
520         || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1))
521         /* Check octet pointer */
522         || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
523         || !TEST_str_eq(p->key, "oct_p")
524         || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
525         || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2))
526         /* Check BN */
527         || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_pub"))
528         || !TEST_str_eq(p->key, "bignumber_pub")
529         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
530         || !TEST_true(OSSL_PARAM_get_BN(p, &bn_pub_res))
531         || !TEST_int_eq(BN_cmp(bn_pub_res, bn_pub), 0)
532         || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_priv"))
533         || !TEST_str_eq(p->key, "bignumber_priv")
534         || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
535         || !TEST_true(OSSL_PARAM_get_BN(p, &bn_priv_res))
536         || !TEST_int_eq(BN_cmp(bn_priv_res, bn_priv), 0))
537         goto err;
538     res = 1;
539 err:
540     OSSL_PARAM_free(params);
541     OSSL_PARAM_free(params_blt);
542     OSSL_PARAM_free(params2_blt);
543     OSSL_PARAM_BLD_free(bld);
544     OSSL_PARAM_BLD_free(bld2);
545     BN_free(bn_priv);
546     BN_free(bn_priv_res);
547     BN_free(bn_pub);
548     BN_free(bn_pub_res);
549     return res;
550 }
551
552 int setup_tests(void)
553 {
554     ADD_ALL_TESTS(template_public_single_zero_test, 2);
555     ADD_ALL_TESTS(template_public_test, 5);
556     /* Only run the secure memory testing if we have secure memory available */
557     if (CRYPTO_secure_malloc_init(1<<16, 16)) {
558         ADD_TEST(template_private_single_zero_test);
559         ADD_ALL_TESTS(template_private_test, 5);
560     }
561     ADD_TEST(builder_limit_test);
562     ADD_TEST(builder_merge_test);
563     return 1;
564 }