]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/param_build_test.c
Update copyright year
[thirdparty/openssl.git] / test / param_build_test.c
CommitLineData
3c93fbac 1/*
3c2bdd7d 2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3c93fbac
P
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>
110bff61 13#include "openssl/param_build.h"
3c93fbac
P
14#include "internal/nelem.h"
15#include "testutil.h"
16
17static int template_public_test(void)
18{
6d4e6009 19 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
3c93fbac 20 OSSL_PARAM *params = NULL, *p;
7312ef3f 21 BIGNUM *bn = NULL, *bn_res = NULL;
3c93fbac
P
22 int i;
23 long int l;
24 int32_t i32;
25 int64_t i64;
26 double d;
5fdaa38f 27 time_t t;
3c93fbac
P
28 char *utf = NULL;
29 const char *cutf;
30 int res = 0;
31
6d4e6009
P
32 if (!TEST_ptr(bld)
33 || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6))
34 || !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42))
35 || !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532))
36 || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999))
5fdaa38f 37 || !TEST_true(OSSL_PARAM_BLD_push_time_t(bld, "t", 11224))
6d4e6009 38 || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875))
7312ef3f
P
39 || !TEST_ptr(bn = BN_new())
40 || !TEST_true(BN_set_word(bn, 1729))
6d4e6009
P
41 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn))
42 || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "utf8_s", "foo",
3c93fbac 43 sizeof("foo")))
6d4e6009 44 || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom",
3c93fbac 45 0))
6d4e6009 46 || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
3c93fbac
P
47 /* Check int */
48 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
49 || !TEST_true(OSSL_PARAM_get_int(p, &i))
50 || !TEST_str_eq(p->key, "i")
51 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
52 || !TEST_size_t_eq(p->data_size, sizeof(int))
53 || !TEST_int_eq(i, -6)
54 /* Check int32 */
55 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
56 || !TEST_true(OSSL_PARAM_get_int32(p, &i32))
57 || !TEST_str_eq(p->key, "i32")
58 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
59 || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
60 || !TEST_int_eq((int)i32, 1532)
61 /* Check int64 */
62 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
63 || !TEST_str_eq(p->key, "i64")
64 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
65 || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
66 || !TEST_true(OSSL_PARAM_get_int64(p, &i64))
67 || !TEST_long_eq((long)i64, -9999999)
68 /* Check long */
69 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
70 || !TEST_str_eq(p->key, "l")
71 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
72 || !TEST_size_t_eq(p->data_size, sizeof(long int))
73 || !TEST_true(OSSL_PARAM_get_long(p, &l))
74 || !TEST_long_eq(l, 42)
5fdaa38f
P
75 /* Check time_t */
76 || !TEST_ptr(p = OSSL_PARAM_locate(params, "t"))
77 || !TEST_str_eq(p->key, "t")
78 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
79 || !TEST_size_t_eq(p->data_size, sizeof(time_t))
80 || !TEST_true(OSSL_PARAM_get_time_t(p, &t))
81 || !TEST_time_t_eq(t, 11224)
3c93fbac
P
82 /* Check double */
83 || !TEST_ptr(p = OSSL_PARAM_locate(params, "d"))
84 || !TEST_true(OSSL_PARAM_get_double(p, &d))
85 || !TEST_str_eq(p->key, "d")
86 || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL)
87 || !TEST_size_t_eq(p->data_size, sizeof(double))
88 || !TEST_double_eq(d, 1.61803398875)
89 /* Check UTF8 string */
90 || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s"))
91 || !TEST_str_eq(p->data, "foo")
92 || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0))
93 || !TEST_str_eq(utf, "foo")
94 /* Check UTF8 pointer */
95 || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
96 || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
7312ef3f
P
97 || !TEST_str_eq(cutf, "bar-boom")
98 /* Check BN */
99 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
100 || !TEST_str_eq(p->key, "bignumber")
101 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
102 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
103 || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
3c93fbac
P
104 goto err;
105 res = 1;
106err:
6d4e6009
P
107 OSSL_PARAM_BLD_free_params(params);
108 OSSL_PARAM_BLD_free(bld);
3c93fbac 109 OPENSSL_free(utf);
7312ef3f
P
110 BN_free(bn);
111 BN_free(bn_res);
3c93fbac
P
112 return res;
113}
114
115static int template_private_test(void)
116{
b7dedba8
P
117 int *data1 = NULL, *data2 = NULL, j;
118 const int data1_num = 12;
119 const int data1_size = data1_num * sizeof(int);
120 const int data2_num = 5;
121 const int data2_size = data2_num * sizeof(int);
122 OSSL_PARAM_BLD *bld = NULL;
3c93fbac 123 OSSL_PARAM *params = NULL, *p;
3c93fbac
P
124 unsigned int i;
125 unsigned long int l;
126 uint32_t i32;
127 uint64_t i64;
128 size_t st;
129 BIGNUM *bn = NULL, *bn_res = NULL;
130 int res = 0;
131
b7dedba8
P
132 if (!TEST_ptr(data1 = OPENSSL_secure_malloc(data1_size))
133 || !TEST_ptr(data2 = OPENSSL_secure_malloc(data2_size))
134 || !TEST_ptr(bld = OSSL_PARAM_BLD_new()))
135 goto err;
136
137 for (j = 0; j < data1_num; j++)
138 data1[j] = -16 * j;
139 for (j = 0; j < data2_num; j++)
140 data2[j] = 2 * j;
141
142 if (!TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6))
6d4e6009
P
143 || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42))
144 || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
145 || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
146 || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
7312ef3f 147 || !TEST_ptr(bn = BN_secure_new())
3c93fbac 148 || !TEST_true(BN_set_word(bn, 1729))
6d4e6009
P
149 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn))
150 || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "oct_s", data1,
b7dedba8 151 data1_size))
6d4e6009 152 || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2,
b7dedba8 153 data2_size))
6d4e6009 154 || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
3c93fbac
P
155 /* Check unsigned int */
156 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
b7dedba8 157 || !TEST_false(CRYPTO_secure_allocated(p->data))
3c93fbac
P
158 || !TEST_true(OSSL_PARAM_get_uint(p, &i))
159 || !TEST_str_eq(p->key, "i")
160 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
161 || !TEST_size_t_eq(p->data_size, sizeof(int))
162 || !TEST_uint_eq(i, 6)
163 /* Check unsigned int32 */
164 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
b7dedba8 165 || !TEST_false(CRYPTO_secure_allocated(p->data))
3c93fbac
P
166 || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
167 || !TEST_str_eq(p->key, "i32")
168 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
169 || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
170 || !TEST_uint_eq((unsigned int)i32, 1532)
171 /* Check unsigned int64 */
172 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
b7dedba8 173 || !TEST_false(CRYPTO_secure_allocated(p->data))
3c93fbac
P
174 || !TEST_str_eq(p->key, "i64")
175 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
176 || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
177 || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
178 || !TEST_ulong_eq((unsigned long)i64, 9999999)
179 /* Check unsigned long int */
180 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
b7dedba8 181 || !TEST_false(CRYPTO_secure_allocated(p->data))
3c93fbac
P
182 || !TEST_str_eq(p->key, "l")
183 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
184 || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
185 || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
186 || !TEST_ulong_eq(l, 42)
187 /* Check size_t */
188 || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
b7dedba8 189 || !TEST_false(CRYPTO_secure_allocated(p->data))
3c93fbac
P
190 || !TEST_str_eq(p->key, "st")
191 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
192 || !TEST_size_t_eq(p->data_size, sizeof(size_t))
193 || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
194 || !TEST_size_t_eq(st, 65537)
195 /* Check octet string */
196 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
b7dedba8 197 || !TEST_true(CRYPTO_secure_allocated(p->data))
3c93fbac
P
198 || !TEST_str_eq(p->key, "oct_s")
199 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
b7dedba8 200 || !TEST_mem_eq(p->data, p->data_size, data1, data1_size)
3c93fbac
P
201 /* Check octet pointer */
202 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
b7dedba8
P
203 || !TEST_false(CRYPTO_secure_allocated(p->data))
204 || !TEST_true(CRYPTO_secure_allocated(*(void **)p->data))
3c93fbac
P
205 || !TEST_str_eq(p->key, "oct_p")
206 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
b7dedba8 207 || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, data2_size)
3c93fbac
P
208 /* Check BN */
209 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
b7dedba8 210 || !TEST_true(CRYPTO_secure_allocated(p->data))
3c93fbac
P
211 || !TEST_str_eq(p->key, "bignumber")
212 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
213 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
b7dedba8 214 || !TEST_int_eq(BN_get_flags(bn, BN_FLG_SECURE), BN_FLG_SECURE)
3c93fbac
P
215 || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
216 goto err;
217 res = 1;
218err:
6d4e6009
P
219 OSSL_PARAM_BLD_free_params(params);
220 OSSL_PARAM_BLD_free(bld);
b7dedba8
P
221 OPENSSL_secure_free(data1);
222 OPENSSL_secure_free(data2);
3c93fbac
P
223 BN_free(bn);
224 BN_free(bn_res);
225 return res;
226}
227
20c98cd4
P
228static int builder_limit_test(void)
229{
230 const int n = 100;
231 char names[100][3];
232 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
233 OSSL_PARAM *params = NULL;
234 int i, res = 0;
235
236 if (!TEST_ptr(bld))
237 goto err;
238
239 for (i = 0; i < n; i++) {
240 names[i][0] = 'A' + (i / 26) - 1;
4f5e206d 241 names[i][1] = 'a' + (i % 26) - 1;
20c98cd4
P
242 names[i][2] = '\0';
243 if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, names[i], 3 * i + 1)))
244 goto err;
245 }
246 if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
247 goto err;
248 /* Count the elements in the params arrary, expecting n */
249 for (i = 0; params[i].key != NULL; i++);
250 if (!TEST_int_eq(i, n))
251 goto err;
252
253 /* Verify that the build, cleared the builder structure */
254 OSSL_PARAM_BLD_free_params(params);
255 params = NULL;
256
257 if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2))
258 || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
259 goto err;
260 /* Count the elements in the params arrary, expecting 1 */
261 for (i = 0; params[i].key != NULL; i++);
262 if (!TEST_int_eq(i, 1))
263 goto err;
264 res = 1;
265err:
266 OSSL_PARAM_BLD_free_params(params);
267 OSSL_PARAM_BLD_free(bld);
268 return res;
269}
270
3c93fbac
P
271int setup_tests(void)
272{
273 ADD_TEST(template_public_test);
b7dedba8
P
274 /* Only run the secure memory testing if we have secure memory available */
275 if (CRYPTO_secure_malloc_init(1<<16, 16))
276 ADD_TEST(template_private_test);
20c98cd4 277 ADD_TEST(builder_limit_test);
3c93fbac
P
278 return 1;
279}