param_build_test.c revision 1.1.1.2 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/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 ;
419 if (!TEST_int_eq(i, n))
420 goto err;
421
422 /* Verify that the build, cleared the builder structure */
423 OSSL_PARAM_free(params);
424 params = NULL;
425
426 if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2))
427 || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
428 goto err;
429 /* Count the elements in the params array, expecting 1 */
430 for (i = 0; params[i].key != NULL; i++)
431 ;
432 if (!TEST_int_eq(i, 1))
433 goto err;
434 res = 1;
435 err:
436 OSSL_PARAM_free(params);
437 OSSL_PARAM_BLD_free(bld);
438 return res;
439 }
440
441 static int builder_merge_test(void)
442 {
443 static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
444 static unsigned char data2[] = { 2, 4, 6, 8, 10 };
445 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
446 OSSL_PARAM_BLD *bld2 = OSSL_PARAM_BLD_new();
447 OSSL_PARAM *params = NULL, *params_blt = NULL, *params2_blt = NULL, *p;
448 unsigned int i;
449 unsigned long int l;
450 uint32_t i32;
451 uint64_t i64;
452 size_t st;
453 BIGNUM *bn_priv = NULL, *bn_priv_res = NULL;
454 BIGNUM *bn_pub = NULL, *bn_pub_res = NULL;
455 int res = 0;
456
457 if (!TEST_ptr(bld)
458 || !TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6))
459 || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42))
460 || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
461 || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
462 || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
463 || !TEST_ptr(bn_priv = BN_secure_new())
464 || !TEST_true(BN_set_word(bn_priv, 1729))
465 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber_priv", bn_priv))
466 || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
467 goto err;
468
469 if (!TEST_ptr(bld2)
470 || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld2, "oct_s", data1,
471 sizeof(data1)))
472 || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld2, "oct_p", data2,
473 sizeof(data2)))
474 || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld2, "i32", 99))
475 || !TEST_ptr(bn_pub = BN_new())
476 || !TEST_true(BN_set_word(bn_pub, 0x42))
477 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld2, "bignumber_pub", bn_pub))
478 || !TEST_ptr(params2_blt = OSSL_PARAM_BLD_to_param(bld2)))
479 goto err;
480
481 if (!TEST_ptr(params = OSSL_PARAM_merge(params_blt, params2_blt)))
482 goto err;
483
484 if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
485 || !TEST_true(OSSL_PARAM_get_uint(p, &i))
486 || !TEST_str_eq(p->key, "i")
487 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
488 || !TEST_size_t_eq(p->data_size, sizeof(int))
489 || !TEST_uint_eq(i, 6)
490 /* Check unsigned int32 */
491 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
492 || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
493 || !TEST_str_eq(p->key, "i32")
494 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
495 || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
496 || !TEST_uint_eq((unsigned int)i32, 99)
497 /* Check unsigned int64 */
498 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
499 || !TEST_str_eq(p->key, "i64")
500 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
501 || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
502 || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
503 || !TEST_ulong_eq((unsigned long)i64, 9999999)
504 /* Check unsigned long int */
505 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
506 || !TEST_str_eq(p->key, "l")
507 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
508 || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
509 || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
510 || !TEST_ulong_eq(l, 42)
511 /* Check size_t */
512 || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
513 || !TEST_str_eq(p->key, "st")
514 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
515 || !TEST_size_t_eq(p->data_size, sizeof(size_t))
516 || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
517 || !TEST_size_t_eq(st, 65537)
518 /* Check octet string */
519 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
520 || !TEST_str_eq(p->key, "oct_s")
521 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
522 || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1))
523 /* Check octet pointer */
524 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
525 || !TEST_str_eq(p->key, "oct_p")
526 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
527 || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2))
528 /* Check BN */
529 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_pub"))
530 || !TEST_str_eq(p->key, "bignumber_pub")
531 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
532 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_pub_res))
533 || !TEST_int_eq(BN_cmp(bn_pub_res, bn_pub), 0)
534 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_priv"))
535 || !TEST_str_eq(p->key, "bignumber_priv")
536 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
537 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_priv_res))
538 || !TEST_int_eq(BN_cmp(bn_priv_res, bn_priv), 0))
539 goto err;
540 res = 1;
541 err:
542 OSSL_PARAM_free(params);
543 OSSL_PARAM_free(params_blt);
544 OSSL_PARAM_free(params2_blt);
545 OSSL_PARAM_BLD_free(bld);
546 OSSL_PARAM_BLD_free(bld2);
547 BN_free(bn_priv);
548 BN_free(bn_priv_res);
549 BN_free(bn_pub);
550 BN_free(bn_pub_res);
551 return res;
552 }
553
554 int setup_tests(void)
555 {
556 ADD_ALL_TESTS(template_public_single_zero_test, 2);
557 ADD_ALL_TESTS(template_public_test, 5);
558 /* Only run the secure memory testing if we have secure memory available */
559 if (CRYPTO_secure_malloc_init(1 << 16, 16)) {
560 ADD_TEST(template_private_single_zero_test);
561 ADD_ALL_TESTS(template_private_test, 5);
562 }
563 ADD_TEST(builder_limit_test);
564 ADD_TEST(builder_merge_test);
565 return 1;
566 }
567