VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.7/test/param_build_test.c@ 97673

最後變更 在這個檔案從97673是 94320,由 vboxsync 提交於 3 年 前

libs/openssl-3.0.1: Export to OSE and fix copyright headers in Makefiles, bugref:10128

檔案大小: 17.1 KB
 
1/*
2 * Copyright 2019-2021 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
17static const OSSL_PARAM params_empty[] = { OSSL_PARAM_END };
18
19static int template_public_test(int tstid)
20{
21 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
22 OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p;
23 BIGNUM *bn = NULL, *bn_res = NULL;
24 int i;
25 long int l;
26 int32_t i32;
27 int64_t i64;
28 double d;
29 time_t t;
30 char *utf = NULL;
31 const char *cutf;
32 int res = 0;
33
34 if (!TEST_ptr(bld)
35 || !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42))
36 || !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532))
37 || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999))
38 || !TEST_true(OSSL_PARAM_BLD_push_time_t(bld, "t", 11224))
39 || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875))
40 || !TEST_ptr(bn = BN_new())
41 || !TEST_true(BN_set_word(bn, 1729))
42 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn))
43 || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "utf8_s", "foo",
44 sizeof("foo")))
45 || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom",
46 0))
47 || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6))
48 || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
49 goto err;
50
51 switch(tstid) {
52 case 0:
53 params = params_blt;
54 break;
55 case 1:
56 params = OSSL_PARAM_merge(params_blt, params_empty);
57 break;
58 case 2:
59 params = OSSL_PARAM_dup(params_blt);
60 break;
61 case 3:
62 p1 = OSSL_PARAM_merge(params_blt, params_empty);
63 params = OSSL_PARAM_dup(p1);
64 break;
65 default:
66 p1 = OSSL_PARAM_dup(params_blt);
67 params = OSSL_PARAM_merge(p1, params_empty);
68 break;
69 }
70 /* Check int */
71 if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
72 || !TEST_true(OSSL_PARAM_get_int(p, &i))
73 || !TEST_str_eq(p->key, "i")
74 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
75 || !TEST_size_t_eq(p->data_size, sizeof(int))
76 || !TEST_int_eq(i, -6)
77 /* Check int32 */
78 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
79 || !TEST_true(OSSL_PARAM_get_int32(p, &i32))
80 || !TEST_str_eq(p->key, "i32")
81 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
82 || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
83 || !TEST_int_eq((int)i32, 1532)
84 /* Check int64 */
85 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
86 || !TEST_str_eq(p->key, "i64")
87 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
88 || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
89 || !TEST_true(OSSL_PARAM_get_int64(p, &i64))
90 || !TEST_long_eq((long)i64, -9999999)
91 /* Check long */
92 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
93 || !TEST_str_eq(p->key, "l")
94 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
95 || !TEST_size_t_eq(p->data_size, sizeof(long int))
96 || !TEST_true(OSSL_PARAM_get_long(p, &l))
97 || !TEST_long_eq(l, 42)
98 /* Check time_t */
99 || !TEST_ptr(p = OSSL_PARAM_locate(params, "t"))
100 || !TEST_str_eq(p->key, "t")
101 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
102 || !TEST_size_t_eq(p->data_size, sizeof(time_t))
103 || !TEST_true(OSSL_PARAM_get_time_t(p, &t))
104 || !TEST_time_t_eq(t, 11224)
105 /* Check double */
106 || !TEST_ptr(p = OSSL_PARAM_locate(params, "d"))
107 || !TEST_true(OSSL_PARAM_get_double(p, &d))
108 || !TEST_str_eq(p->key, "d")
109 || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL)
110 || !TEST_size_t_eq(p->data_size, sizeof(double))
111 || !TEST_double_eq(d, 1.61803398875)
112 /* Check UTF8 string */
113 || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s"))
114 || !TEST_str_eq(p->data, "foo")
115 || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0))
116 || !TEST_str_eq(utf, "foo")
117 /* Check UTF8 pointer */
118 || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
119 || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
120 || !TEST_str_eq(cutf, "bar-boom")
121 /* Check BN */
122 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
123 || !TEST_str_eq(p->key, "bignumber")
124 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
125 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
126 || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
127 goto err;
128 res = 1;
129err:
130 OPENSSL_free(p1);
131 if (params != params_blt)
132 OPENSSL_free(params);
133 OSSL_PARAM_free(params_blt);
134 OSSL_PARAM_BLD_free(bld);
135 OPENSSL_free(utf);
136 BN_free(bn);
137 BN_free(bn_res);
138 return res;
139}
140
141static int template_private_test(int tstid)
142{
143 int *data1 = NULL, *data2 = NULL, j;
144 const int data1_num = 12;
145 const int data1_size = data1_num * sizeof(int);
146 const int data2_num = 5;
147 const int data2_size = data2_num * sizeof(int);
148 OSSL_PARAM_BLD *bld = NULL;
149 OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p;
150 unsigned int i;
151 unsigned long int l;
152 uint32_t i32;
153 uint64_t i64;
154 size_t st;
155 BIGNUM *bn = NULL, *bn_res = NULL;
156 int res = 0;
157
158 if (!TEST_ptr(data1 = OPENSSL_secure_malloc(data1_size))
159 || !TEST_ptr(data2 = OPENSSL_secure_malloc(data2_size))
160 || !TEST_ptr(bld = OSSL_PARAM_BLD_new()))
161 goto err;
162
163 for (j = 0; j < data1_num; j++)
164 data1[j] = -16 * j;
165 for (j = 0; j < data2_num; j++)
166 data2[j] = 2 * j;
167
168 if (!TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6))
169 || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42))
170 || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
171 || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
172 || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
173 || !TEST_ptr(bn = BN_secure_new())
174 || !TEST_true(BN_set_word(bn, 1729))
175 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn))
176 || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "oct_s", data1,
177 data1_size))
178 || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2,
179 data2_size))
180 || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
181 goto err;
182 switch(tstid) {
183 case 0:
184 params = params_blt;
185 break;
186 case 1:
187 params = OSSL_PARAM_merge(params_blt, params_empty);
188 break;
189 case 2:
190 params = OSSL_PARAM_dup(params_blt);
191 break;
192 case 3:
193 p1 = OSSL_PARAM_merge(params_blt, params_empty);
194 params = OSSL_PARAM_dup(p1);
195 break;
196 default:
197 p1 = OSSL_PARAM_dup(params_blt);
198 params = OSSL_PARAM_merge(p1, params_empty);
199 break;
200 }
201 /* Check unsigned int */
202 if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
203 || !TEST_false(CRYPTO_secure_allocated(p->data))
204 || !TEST_true(OSSL_PARAM_get_uint(p, &i))
205 || !TEST_str_eq(p->key, "i")
206 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
207 || !TEST_size_t_eq(p->data_size, sizeof(int))
208 || !TEST_uint_eq(i, 6)
209 /* Check unsigned int32 */
210 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
211 || !TEST_false(CRYPTO_secure_allocated(p->data))
212 || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
213 || !TEST_str_eq(p->key, "i32")
214 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
215 || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
216 || !TEST_uint_eq((unsigned int)i32, 1532)
217 /* Check unsigned int64 */
218 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
219 || !TEST_false(CRYPTO_secure_allocated(p->data))
220 || !TEST_str_eq(p->key, "i64")
221 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
222 || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
223 || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
224 || !TEST_ulong_eq((unsigned long)i64, 9999999)
225 /* Check unsigned long int */
226 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
227 || !TEST_false(CRYPTO_secure_allocated(p->data))
228 || !TEST_str_eq(p->key, "l")
229 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
230 || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
231 || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
232 || !TEST_ulong_eq(l, 42)
233 /* Check size_t */
234 || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
235 || !TEST_false(CRYPTO_secure_allocated(p->data))
236 || !TEST_str_eq(p->key, "st")
237 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
238 || !TEST_size_t_eq(p->data_size, sizeof(size_t))
239 || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
240 || !TEST_size_t_eq(st, 65537)
241 /* Check octet string */
242 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
243 || !TEST_true(CRYPTO_secure_allocated(p->data))
244 || !TEST_str_eq(p->key, "oct_s")
245 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
246 || !TEST_mem_eq(p->data, p->data_size, data1, data1_size)
247 /* Check octet pointer */
248 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
249 || !TEST_false(CRYPTO_secure_allocated(p->data))
250 || !TEST_true(CRYPTO_secure_allocated(*(void **)p->data))
251 || !TEST_str_eq(p->key, "oct_p")
252 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
253 || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, data2_size)
254 /* Check BN */
255 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
256 || !TEST_true(CRYPTO_secure_allocated(p->data))
257 || !TEST_str_eq(p->key, "bignumber")
258 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
259 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
260 || !TEST_int_eq(BN_get_flags(bn, BN_FLG_SECURE), BN_FLG_SECURE)
261 || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
262 goto err;
263 res = 1;
264err:
265 OSSL_PARAM_free(p1);
266 if (params != params_blt)
267 OSSL_PARAM_free(params);
268 OSSL_PARAM_free(params_blt);
269 OSSL_PARAM_BLD_free(bld);
270 OPENSSL_secure_free(data1);
271 OPENSSL_secure_free(data2);
272 BN_free(bn);
273 BN_free(bn_res);
274 return res;
275}
276
277static int builder_limit_test(void)
278{
279 const int n = 100;
280 char names[100][3];
281 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
282 OSSL_PARAM *params = NULL;
283 int i, res = 0;
284
285 if (!TEST_ptr(bld))
286 goto err;
287
288 for (i = 0; i < n; i++) {
289 names[i][0] = 'A' + (i / 26) - 1;
290 names[i][1] = 'a' + (i % 26) - 1;
291 names[i][2] = '\0';
292 if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, names[i], 3 * i + 1)))
293 goto err;
294 }
295 if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
296 goto err;
297 /* Count the elements in the params arrary, expecting n */
298 for (i = 0; params[i].key != NULL; i++);
299 if (!TEST_int_eq(i, n))
300 goto err;
301
302 /* Verify that the build, cleared the builder structure */
303 OSSL_PARAM_free(params);
304 params = NULL;
305
306 if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2))
307 || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
308 goto err;
309 /* Count the elements in the params arrary, expecting 1 */
310 for (i = 0; params[i].key != NULL; i++);
311 if (!TEST_int_eq(i, 1))
312 goto err;
313 res = 1;
314err:
315 OSSL_PARAM_free(params);
316 OSSL_PARAM_BLD_free(bld);
317 return res;
318}
319
320static int builder_merge_test(void)
321{
322 static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
323 static unsigned char data2[] = { 2, 4, 6, 8, 10 };
324 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
325 OSSL_PARAM_BLD *bld2 = OSSL_PARAM_BLD_new();
326 OSSL_PARAM *params = NULL, *params_blt = NULL, *params2_blt = NULL, *p;
327 unsigned int i;
328 unsigned long int l;
329 uint32_t i32;
330 uint64_t i64;
331 size_t st;
332 BIGNUM *bn_priv = NULL, *bn_priv_res = NULL;
333 BIGNUM *bn_pub = NULL, *bn_pub_res = NULL;
334 int res = 0;
335
336 if (!TEST_ptr(bld)
337 || !TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6))
338 || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42))
339 || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
340 || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
341 || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
342 || !TEST_ptr(bn_priv = BN_secure_new())
343 || !TEST_true(BN_set_word(bn_priv, 1729))
344 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber_priv", bn_priv))
345 || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
346 goto err;
347
348 if (!TEST_ptr(bld2)
349 || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld2, "oct_s", data1,
350 sizeof(data1)))
351 || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld2, "oct_p", data2,
352 sizeof(data2)))
353 || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld2, "i32", 99))
354 || !TEST_ptr(bn_pub = BN_new())
355 || !TEST_true(BN_set_word(bn_pub, 0x42))
356 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld2, "bignumber_pub", bn_pub))
357 || !TEST_ptr(params2_blt = OSSL_PARAM_BLD_to_param(bld2)))
358 goto err;
359
360 if (!TEST_ptr(params = OSSL_PARAM_merge(params_blt, params2_blt)))
361 goto err;
362
363 if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
364 || !TEST_true(OSSL_PARAM_get_uint(p, &i))
365 || !TEST_str_eq(p->key, "i")
366 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
367 || !TEST_size_t_eq(p->data_size, sizeof(int))
368 || !TEST_uint_eq(i, 6)
369 /* Check unsigned int32 */
370 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
371 || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
372 || !TEST_str_eq(p->key, "i32")
373 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
374 || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
375 || !TEST_uint_eq((unsigned int)i32, 99)
376 /* Check unsigned int64 */
377 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
378 || !TEST_str_eq(p->key, "i64")
379 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
380 || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
381 || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
382 || !TEST_ulong_eq((unsigned long)i64, 9999999)
383 /* Check unsigned long int */
384 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
385 || !TEST_str_eq(p->key, "l")
386 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
387 || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
388 || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
389 || !TEST_ulong_eq(l, 42)
390 /* Check size_t */
391 || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
392 || !TEST_str_eq(p->key, "st")
393 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
394 || !TEST_size_t_eq(p->data_size, sizeof(size_t))
395 || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
396 || !TEST_size_t_eq(st, 65537)
397 /* Check octet string */
398 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
399 || !TEST_str_eq(p->key, "oct_s")
400 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
401 || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1))
402 /* Check octet pointer */
403 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
404 || !TEST_str_eq(p->key, "oct_p")
405 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
406 || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2))
407 /* Check BN */
408 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_pub"))
409 || !TEST_str_eq(p->key, "bignumber_pub")
410 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
411 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_pub_res))
412 || !TEST_int_eq(BN_cmp(bn_pub_res, bn_pub), 0)
413 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_priv"))
414 || !TEST_str_eq(p->key, "bignumber_priv")
415 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
416 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_priv_res))
417 || !TEST_int_eq(BN_cmp(bn_priv_res, bn_priv), 0))
418 goto err;
419 res = 1;
420err:
421 OSSL_PARAM_free(params);
422 OSSL_PARAM_free(params_blt);
423 OSSL_PARAM_free(params2_blt);
424 OSSL_PARAM_BLD_free(bld);
425 OSSL_PARAM_BLD_free(bld2);
426 BN_free(bn_priv);
427 BN_free(bn_priv_res);
428 BN_free(bn_pub);
429 BN_free(bn_pub_res);
430 return res;
431}
432
433int setup_tests(void)
434{
435 ADD_ALL_TESTS(template_public_test, 5);
436 /* Only run the secure memory testing if we have secure memory available */
437 if (CRYPTO_secure_malloc_init(1<<16, 16))
438 ADD_ALL_TESTS(template_private_test, 5);
439 ADD_TEST(builder_limit_test);
440 ADD_TEST(builder_merge_test);
441 return 1;
442}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette