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