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