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